Java - Clip重新开放后不会发挥作用

时间:2015-02-28 04:27:19

标签: java audio clip

我正在使用javax.sound api播放音频剪辑,它第一次工作正常,但在我关闭剪辑后,重新打开它并尝试再次播放我什么都没得到。如果我不关闭它,它会重播很好但我更喜欢在完成播放后关闭所有媒体。我需要改变什么?我错过了什么?

public class MediaClip {

protected boolean loop = false;
protected AudioInputStream audioStream;
protected Clip clip;
protected LineListener listener;

public MediaClip(AudioInputStream inAudioStream, boolean doesLoop) {
    audioStream = inAudioStream;
    loop = doesLoop;

    try {
        clip = AudioSystem.getClip();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    }

    listener = new LineListener() {

        @Override
        public void update(LineEvent event) {
            if (event.getType() != LineEvent.Type.STOP) {
                return;
            }

            if(clip == null) {
                return;
            }

            clip.close();
        }
    };
}

public void play() {
    boolean playing = clip.isRunning();
    if(playing == true) {
        return;
    }

    boolean lineOpen = clip.isOpen();
    if(lineOpen == false) {
        openLine();
    }

    clip.setFramePosition(0);
    clip.start();
}

private void openLine() {
    try {
        clip.open(audioStream);
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(loop == true) {
        clip.setLoopPoints(0, -1);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }

    clip.addLineListener(listener);
}
}

0 个答案:

没有答案