在循环中连续播放音乐

时间:2014-06-29 09:30:25

标签: 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)

根据文档,start会让玩家:

  

开始播放流。流将继续播放,直到流用完数据,或者它已停止。

因此,你可以将它包装成无限循环:

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

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