gpt4 book ai didi

How to play an MP3 file stored in resources folder in Kotlin Multiplatform with Jetpack Compose for Desktop?(如何用Jetpack Compose for Desktop播放Kotlin多平台资源文件夹中存储的MP3文件?)

转载 作者:bug小助手 更新时间:2023-10-28 13:35:00 24 4
gpt4 key购买 nike



I'm trying to play a sound in my Compose for Desktop project when pressing a button. My MP3 file is stored in the resources folder (./src/jvmMain/resources/beep.mp3).

当我按下一个按钮时,我正在尝试播放我的Compose for Desktop项目中的声音。我的MP3文件存储在Resources文件夹(./src/jvmMain/Resources/beep.mp3)中。


I've been trying using the useResource function as follows (inside onClick parameter of a @Composable Button):

我一直在尝试使用useResource函数,如下所示(在@Composable Button的onClick参数中):


scope.launch {
useResource("beep.mp3") {
val clip = AudioSystem.getClip()
val audioInputStream = AudioSystem.getAudioInputStream(
it
)
clip.open(audioInputStream)
clip.start()
}
}

but I get an error: Exception in thread "AWT-EventQueue-0" java.io.IOException: mark/reset not supported. I got the same error if I don't use the scope, which I define at the top level of my composable as:

但是我得到一个错误:在线程“awt-EventQueue-0”中出现异常。如果我不使用作用域,我会得到相同的错误,我在组合的顶层定义为:


val scope = rememberCoroutineScope()

Any help will be appreciated!

任何帮助我们都将不胜感激!


更多回答

It looks quite simillar to this one issue stackoverflow.com/a/5529906/11207039. Does this solution work for you?

它看起来非常类似于这一期stackoverflow.com/a/5529906/11207039。这个解决方案对你有效吗?

It works but only with WAV files, not MP3 files.

它只适用于wav文件,不适用于mp3文件。

优秀答案推荐

A library is needed to decode MP3 files. This is the key. You may add this maven dependency:

需要一个库来解码MP3文件。这是关键。您可以添加这个maven依赖项:


implementation("com.googlecode.soundlibs:mp3spi:1.9.5.4")

Using Java Sound APIs you originally used:

使用您最初使用的Java Sound API:


import java.io.File
import javax.sound.sampled.AudioFormat
import javax.sound.sampled.AudioInputStream
import javax.sound.sampled.AudioSystem
import javax.sound.sampled.AudioSystem.getAudioInputStream
import javax.sound.sampled.DataLine.Info
import javax.sound.sampled.SourceDataLine

class AudioPlayer {
fun play(path: String) {
val file = File(path)
getAudioInputStream(file).use { `in` ->
val outFormat = getOutFormat(`in`.format)
val info = Info(SourceDataLine::class.java, outFormat)
AudioSystem.getLine(info).use { line ->
(line as? SourceDataLine)?.let { l ->
l.open(outFormat)
l.start()
stream(getAudioInputStream(outFormat, `in`), l)
l.drain()
l.stop()
}
}
}
}

private fun getOutFormat(inFormat: AudioFormat): AudioFormat {
val ch = inFormat.channels
val rate = inFormat.sampleRate
return AudioFormat(AudioFormat.Encoding.PCM_SIGNED, rate, 16, ch, ch * 2, rate, false)
}

private fun stream(`in`: AudioInputStream, line: SourceDataLine) {
val buffer = ByteArray(65536)
var n = 0
while (n != -1) {
line.write(buffer, 0, n)
n = `in`.read(buffer, 0, buffer.size)
}
}
}

And call the play method from a non-UI thread / coroutine:

并从非UI线程/协程调用Play方法:


AudioPlayer().play(fullPathToMP3File)

Credit to original work by oldo: https://odoepner.wordpress.com/2013/07/19/play-mp3-or-ogg-using-javax-sound-sampled-mp3spi-vorbisspi/

归功于奥多的原创作品:https://odoepner.wordpress.com/2013/07/19/play-mp3-or-ogg-using-javax-sound-sampled-mp3spi-vorbisspi/


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com