音频剪辑不会连续循环
谁能指出我正确的方向,为什么这段代码不会连续播放这个音频剪辑?它播放一次然后停止。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在运行更大的应用程序,则此答案可能不适用。但对于仅使用该代码段的简单测试,此可能有帮助:
Clip.loop() 启动它自己的线程,但该线程不会使 JVM 保持活动状态。因此,为了使其正常工作,请确保夹子不是唯一的线程。
如果我从这个片段中省略 Thread.sleep(..) ,我会遇到和你一样的问题;
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;
为了播放完整的音频文件,您需要知道睡眠的时间长度。另一种选择是:
这会保持睡眠状态(以 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:
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.
我的音频文件包含 20 秒的警报声。我需要它不断地响。我没有使用线程,而是继续使用下面所示的代码。
认为这会有所帮助。谢谢。
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.
Think this would help. Thanks.