音频剪辑不会连续循环

发布于 2024-12-28 16:46:11 字数 274 浏览 4 评论 0原文

谁能指出我正确的方向,为什么这段代码不会连续播放这个音频剪辑?它播放一次然后停止。

final Clip clip = AudioSystem.getClip();
final AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("Alarm_Police.wav"));
clip.open(inputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);

Can anyone point me in the right direction as to why this code will not play this audio clip continuously? It plays it once and stops.

final Clip clip = AudioSystem.getClip();
final AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("Alarm_Police.wav"));
clip.open(inputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我喜欢麦丽素 2025-01-04 16:46:11

如果您正在运行更大的应用程序,则此答案可能不适用。但对于仅使用该代码段的简单测试,此可能有帮助:

Clip.loop() 启动它自己的线程,但该线程不会使 JVM 保持活动状态。因此,为了使其正常工作,请确保夹子不是唯一的线程。

如果我从这个片段中省略 Thread.sleep(..) ,我会遇到和你一样的问题;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Snippet {
    public static void main(String[] args) throws Exception {

        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("notify.wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        Thread.sleep(10000); // looping as long as this thread is alive
    }
}

If you are running a bigger application, this answer may not apply. But for a simple test with only that piece of code, this may help:

Clip.loop() starts it's own thread, but that thread will not keep the JVM alive. So to make it work, make sure the clip is not the only thread.

If I leave out Thread.sleep(..) from this snippet, I get the same issue as you;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Snippet {
    public static void main(String[] args) throws Exception {

        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("notify.wav"));
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        Thread.sleep(10000); // looping as long as this thread is alive
    }
}
つ可否回来 2025-01-04 16:46:11

为了播放完整的音频文件,您需要知道睡眠的时间长度。另一种选择是:

while(clip.isRunning())
{
  Thread.sleep(100);
}

这会保持睡眠状态(以 100 毫秒为增量),直到 isRunning() 状态变为 false。
您可能需要在此循环之前进行初始睡眠,以便 isRunning() 状态有时间设置。

In order to play the complete audio file you would need to know the length of time to sleep. An alternative would be to:

while(clip.isRunning())
{
  Thread.sleep(100);
}

This keeps sleeping (in 100ms increments) until the isRunning() state has turned to false.
You may need an initial sleep before this loop so that the isRunning() state has time to set.

狂之美人 2025-01-04 16:46:11

我的音频文件包含 20 秒的警报声。我需要它不断地响。我没有使用线程,而是继续使用下面所示的代码。

        while(true){
            clip.start();
            clip.loop(clip.LOOP_CONTINUOUSLY);              
        } 

认为这会有所帮助。谢谢。

My audio file contains 20 seconds of an alarm beep. I need it continuously to ring. Instead of using thread, i went on with the piece of code shown below.

        while(true){
            clip.start();
            clip.loop(clip.LOOP_CONTINUOUSLY);              
        } 

Think this would help. Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文