首页 > 解决方案 > Coroutine - Head First Kotlin(鼓机)中的引用代码

问题描述

嗨我想知道为什么当我注释掉声音文件时只播放一次

Thread.sleep(100 * (part.length + 1L))

从下面的代码

fun main() {
    playBeats("x-x-x-x-x-x-", "toms.aiff")
}

fun playBeats(beats: String, file: String) {
    val parts = beats.split("x")
    println(parts)
    var count = 0
    for (part in parts) {
        count += part.length + 1
        if (part == "") {
            playSound(file)
        } else {
            Thread.sleep(100 * (part.length + 1L))
            if (count < beats.length) {
                playSound(file)
            }
        }
    }
}

标签: kotlin-coroutines

解决方案


推荐阅读