我有这个AudioTrack类,它以字节串形式获取音频数据流。我为此使用了AsyncTask,它工作正常。但是当我搬到科特林时,我无法使它工作。在AsychTask中,数据被顺序写入,并且音频播放流畅。但是在Kotlin中,当我播放音频时,声音混杂了。
我尝试使用暂停,异步操作,但失败了。
Java-
class PlayAsynchTask : AsyncTask<Void, Void, String?>() {
var byteString: ByteString? = null
override fun doInBackground(vararg params: Void?): String? {
val data = byteString?.toByteArray()
if (audioTrack != null && data != null) {
if (audioTrack?.state == AudioTrack.PLAYSTATE_PAUSED) {
audioTrack?.play()
}
try {
audioTrack?.write(data, 0, data.size)
} catch (ex: Exception) {
}
}
return null
}
}
科特琳
fun wavPlay(byteString: ByteString?) {
val wavData: ByteArray? = byteString?.toByteArray()
aPlayJob = GlobalScope.launch {
audioTrack?.play()
audioTrack?.write(wavData!!, 0, wavData.size)
}
}