如何在 Kotlin JVM 桌面上播放声音文件?

问题描述 投票:0回答:1

重要说明 - 我正在编写 JVM 桌面应用程序。

我试过 KorAU 库,但文档缺少示例,它总是失败,并出现以下错误:

fun main() {
  val file = File("path/that/exists.mp3")
  runBlocking {
    nativeSoundProvider.createSound(data=file.readBytes()).play()
  }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4608 out of bounds for length 4608

我试过猜测其他方法,但我最终得到了一个

AudioFormatException

kotlin audio mp3
1个回答
0
投票

根据文档,我在 Windows 的 JVM 桌面应用程序中尝试了这个并且它有效。

suspend fun play() {
    val file = File("mymusic.mp3")
    if (file.exists())
            file.toVfs().readAudioStream().playAndWait()
}
© www.soinside.com 2019 - 2024. All rights reserved.