通过Clip和AudioInputStream重放Java声音不起作用

时间:2021-08-14 19:46:05

This is a slightly modified example from the Java Sound info page. https://*.com/tags/javasound/info Unfortunately, it only plays the sound once but the intention is twice.

这是Java声音信息页面中略微修改的示例。 https://*.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();
    }
}

Edit: To understand the answer, see the link provided by Wanderlust or just do what it says in the comment below his answer.

编辑:要了解答案,请参阅Wanderlust提供的链接,或者只是按照他的回答下面的评论中的说法进行操作。

1 个解决方案

#1


0  

For playing the clip for the second time you must call

要第二次播放剪辑,您必须拨打电话

clip.start();
clip.stop();

cause after the second call of clip.start(); it is trying to play the file from the place where it stopped previously.

在第二次调用clip.start()之后导致它试图从之前停止的地方播放文件。

#1


0  

For playing the clip for the second time you must call

要第二次播放剪辑,您必须拨打电话

clip.start();
clip.stop();

cause after the second call of clip.start(); it is trying to play the file from the place where it stopped previously.

在第二次调用clip.start()之后导致它试图从之前停止的地方播放文件。