2012-02-24 101 views
3

我想從我的scala 1.9.2程序播放.wav文件。我能看到的唯一方法就是導入sun.audio。有沒有更好的辦法?在scala中播放wav文件

+2

見http://stackoverflow.com/questions/7863860/is-there-an-equivalent-of-the-java-sound- api – 2012-02-24 22:07:34

+0

回到java是一個很好的選擇。 – 2012-02-25 09:52:14

回答

4
import java.net.URL 
import javax.sound.sampled._ 

val url = new URL("http://mywebpages.comcast.net/jdeshon2/wave_files/jad0001a.wav") 
val audioIn = AudioSystem.getAudioInputStream(url) 
val clip = AudioSystem.getClip 
clip.open(audioIn) 
clip.start 
1

如果以字節爲單位的音頻

def play(bytes: Array[Byte]) { 
    import sun.audio.{AudioPlayer, AudioStream} 
    val audioStream = new AudioStream(new ByteArrayInputStream(bytes)) 
    AudioPlayer.player.start(audioStream) 
}