在java中循环播放音乐

时间:2014-06-29 15:06:23

标签: java

我已经编写了这段代码,用于在我的java程序中播放音乐,但我想在循环中连续播放音乐,因为我尝试了无限循环,但它无法正常工作。请告诉我们如何连续播放音乐?

import java.io.FileInputStream;
import sun.audio.*;

public class A {


    public static void main(String arg[]) throws Exception {       

        AudioPlayer MGP = AudioPlayer.player;
        AudioStream BGM = new AudioStream(new FileInputStream("sounds.wav"));
        AudioPlayer.player.start(BGM);
    }
}

1 个答案:

答案 0 :(得分:0)

来自JavaDoc:

要播放连续声音,首先必须创建一个AudioData实例并使用它来构造一个ContinuousAudioDataStream。例如:

 AudioData data = new AudioStream(url.openStream()).getData();
 ContinuousAudioDataStream audiostream = new ContinuousAudioDataStream(data);
 AudioPlayer.player.start(audiostream);

我真的不认为调整文档会很困难。

import java.io.FileInputStream;
import sun.audio.*;

public class A {

    public static void main(String arg[]) throws Exception {    

        AudioData data = new AudioStream(new FileInputStream("yourfile.wav")).getData();
        ContinuousAudioDataStream BGM = new ContinuousAudioDataStream(data);
        AudioPlayer.player.start(BGM);
    }
}