Java Sound播放一次然后不再播放

标签 java audio

我有一个游戏需要能够播放声音。当我点击一个按钮时,声音会播放,但当我再次点击该按钮时,声音不会播放。

下面是我的声音等级:

public class Sound {

  public static final Sound select = new Sound("/selectBTN.wav");
  public static final Sound scroll = new Sound("/btn.wav");

  //Phone calls
  public static final Sound call1 = new Sound("/calls/call1.wav");
  public static final Sound call2 = new Sound("/calls/call2.wav");
  public static final Sound call3 = new Sound("/calls/call3.wav");


  private Clip c;

  public Sound(final String filename) {
    try {
         c = AudioSystem.getClip();
         final AudioInputStream inputStream AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(filename));
         c.open(inputStream);
         c.addLineListener(new LineListener() {

           @Override
           public void update(final LineEvent event) {
              if (event.getType().equals(Type.STOP)) {
                   System.out.println("Do something");
               }
            }
           });
        } catch (final Exception e) {
           e.printStackTrace();
       }
  }

  public void play() {
     try {
        new Thread() {
            public void run() {
              if (!title.mute) {
                c.start();
               }
            }
          }.start();
     } catch (Exception e) {
          e.printStackTrace();
     }
  }
}

我使用这一行 Sound.call1.play() 来播放声音。谁能帮我解决这个问题?

最佳答案

尝试从头开始寻找剪辑。

c.setFramePosition(0);
c.start();

否则,您第二次调用 start 时将无法播放,因为 Clip 已结束。

关于Java Sound播放一次然后不再播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583006/

相关文章:

javax.swing.text.AbstractDocument$LeafElement 包含无效的 p0/p1 值

java - ejb2 无状态 session bean 中的同步块(synchronized block)

java - 无法在 Android 中将 Open CV 垫从 native C++ 返回到 Java

java - 无法弄清楚为什么它会抛出 ConcurrentModificationException

ios - 如何将实时音频从iOS设备流传输到Wowza媒体服务器?

c++ - 在 C++ 中使用 SDL 的简单声波发生器

java - 读入 WAV 文件并计算 RMS

c++ - 带有音频的 Libavcodec

Qt MediaPlayer 从内存中播放音频

java - propertyplaceholderconfigurer类名替换问题