通过 Clip 和 AudioInputStream 重放 Java 声音不起作用
这是 Java Sound 信息页面的一个稍微修改过的示例。 https://stackoverflow.com/tags/javasound/info 不幸的是,它只播放声音一次,但意图是两次。
import java.io.File;
import javax.sound.sampled.*;
public class TestNoise {
public static void main(String[] args) throws Exception {
File f = new File("/home/brian/drip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(ais);
clip.start(); // heard this
Java.killTime();
clip.start(); // NOT HEARD
Java.killTime();
}
}
编辑:要了解答案,请参阅 Wanderlust 提供的链接,或者按照其答案下方评论中的说明进行操作。
This is a slightly modified example from the Java Sound info page. https://stackoverflow.com/tags/javasound/info Unfortunately, it only plays the sound once but the intention is twice.
import java.io.File;
import javax.sound.sampled.*;
public class TestNoise {
public static void main(String[] args) throws Exception {
File f = new File("/home/brian/drip.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, af);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(ais);
clip.start(); // heard this
Java.killTime();
clip.start(); // NOT HEARD
Java.killTime();
}
}
Edit: To understand the answer, see the link provided by Wanderlust or just do what it says in the comment below his answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个美妙的 API 对我有用:
This wonderful API worked for me:
要第二次播放剪辑,您必须
在第二次调用clip.start()后调用cause;它试图从
For playing the clip for the second time you must call
cause after the second call of clip.start(); it is trying to play the file from the place where it stopped previously.