如何让音频文件连续重复?现在音乐只播放一次,那就是它。我试图让声音一遍又一遍地重复。我是否必须以另一种方式获取音频文件或者是否有更简单的方法?
代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound {
public static void music(String fileName) {
try {
InputStream in = new FileInputStream(new File(fileName));
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我怎么称呼它:
// Plays the music.
public void music() {
Sound.music("res/music/I Like Your Hat.wav");
}
答案 0 :(得分:2)
sun.*
个软件包,这些软件包是私有软件包,可能并不总是可用。请查看this example,了解有关如何播放音频片段的一些想法。 Clip
和Clip#loop
作为一个非常简单的例子......
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(...);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();