转自:点击打开链接
使用sun.audio.*可以很方便的使用java播放声音文件,例如下面这个简单的例子就可以播放声音了
package test; import java.io.FileInputStream; import sun.audio.AudioPlayer; import sun.audio.AudioStream; public class AudioTest { public static void main(String[] args) throws Exception { AudioStream as = new AudioStream(new FileInputStream("sound.wav")); AudioPlayer.player.start(as); } }
声音文件的停止也很简单
AudioPlayer.player.stop(as);
如果循环播放和循环停止可以这么写
AudioData ad = as.getData();
ContinuousAudioDataStream cads = new ContinuousAudioDataStream(ad);
//循环播放开始哦
AudioPlayer.player.start(cads);
//循环播放停止
AudioPlayer.player.stop(cads);