简体   繁体   中英

Stop previously played WAV sound

I am making a desktop Softphone-like application in Java. I want to add the dialed-number sounds (DTMF). For example, when I dial 51, it plays dtmf5.wav after pressing the 5, but then it plays dtmf1.wav when I press the 1, without stopping the 5's tone. How do I make sure only one sound - the latest one - plays at once?

This is what I'm using to play the sound:

private void lbl01MouseReleased(java.awt.event.MouseEvent evt)         {                                    
    String val=txtdisplay.getText() + "1";
    txtdisplay.setText(val);
    if (evt.getSource() == lbl01)  {
        URL urlClick = StartGUI.class.getResource("/Dtmf/dtmf1.wav");
        AudioClip click = Applet.newAudioClip(urlClick);
        click.play();
    }
}               

To stop any previously playing audio, you can keep a reference to the AudioClip when it is created. When the action is sent to play a Clip just stop the previous clip by calling it's stop method.

AudioClip currentAudio = null;

...

private void lbl01MouseReleased(java.awt.event.MouseEvent evt){                                    
    String val=txtdisplay.getText() +"1";
    txtdisplay.setText(val);
    if ( currentAudio != null ){
        currentAudio.stop();
    }
    if (evt.getSource() == lbl01) 
    {

        URL urlClick = StartGUI.class.getResource("/Dtmf/dtmf1.wav");
        currentAudio = Applet.newAudioClip(urlClick);
        currentAudio.play();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM