英文:
How to loop the audio file: play again after it ends javax.sound.sampled
问题
我正在尝试一遍又一遍地播放声音。我有这段代码:
public void play() {
try {
URL defaultSound = getClass().getResource(filename);
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(defaultSound);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start( );
System.out.println(clip.getMicrosecondLength());
Thread.sleep(clip.getMicrosecondLength() / 1000);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
try {
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
但它只播放声音一次。
英文:
I'm trying to play a sound over and over. I have this code:
public void play() {
try {
URL defaultSound = getClass().getResource(filename);
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(defaultSound);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start( );
System.out.println(clip.getMicrosecondLength());
Thread.sleep(clip.getMicrosecondLength() / 1000);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
try {
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
catch (Exception e) {
e.printStackTrace();
}
}
but it only plays the sound once.
答案1
得分: 1
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY); // <- 新增!
clip.start();
> 参数:<br>
count
- 播放应该从循环的结束位置回到循环的开始位置的次数,或者使用 LOOP_CONTINUOUSLY
表示循环应该一直持续,直到被中断。
英文:
clip.open(audioInputStream);
clip.start( );
Should be:
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY); // <- NEW!
clip.start( );
See Clip.loop(int)
:
> Parameters:<br>
count
- the number of times playback should loop back from the loop's end position to the loop's start position, or LOOP_CONTINUOUSLY
to indicate that looping should continue until interrupted
答案2
得分: 0
你可能需要使用Clip#setFramePosition来设置Clip的frame position
为0。在调用Clip#start
之前,你需要调用这个方法。你还需要检查LineEvent
的类型是否为值LineEvent.Type#STOP,以确保事件是一个更新事件或关闭事件,确实是在停止时发生的。
@Override
public void update(LineEvent event) {
try {
if (event.getType() == LineEvent.Type.STOP) {
clip.setFramePosition(0);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
英文:
You're going to want to likely set the Clip's frame position
using Clip#setFramePosition to 0. You will want to call this before Clip#start
. You also need to check if the LineEvent
type is the value LineEvent.Type#STOP to ensure the event is an update event or close, and it is indeed when it stops.
@Override
public void update(LineEvent event) {
try {
if (event.getType() == LineEvent.Type.STOP) {
clip.setFramePosition(0);
clip.start();
Thread.sleep(clip.getMicrosecondLength() / 1000);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论