如何循环播放音频文件:在结束后再次播放 javax.sound.sampled

huangapple go评论75阅读模式
英文:

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();

查看Clip.loop(int)

> 参数:<br>
count - 播放应该从循环的结束位置回到循环的开始位置的次数,或者使用 LOOP_CONTINUOUSLY 表示循环应该一直持续,直到被中断

英文:
clip.open(audioInputStream);
clip.start( );

Should be:

clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY); // &lt;- 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();
    }
}

huangapple
  • 本文由 发表于 2020年8月4日 22:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定