Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying single audio file at any one time
    primarykey
    data
    text
    <p>How to play only 1 audio file at any one time?</p> <p>So I've created a GUI which has multiple buttons, and to each button is attached an action listener. When the button is pressed, it will play the audio file (.wav). This works fine. The issue is that I want it to only play one file at any one time, so the sounds do not overlap, and I do not know what to do for this.</p> <p>Will I need to use threading and synchronization? Any and all advice will be much appreciated. </p> <p>EDIT </p> <p>Declared as class variables:</p> <pre><code>String soundName; AudioInputStream audioInputStream; Clip clip; </code></pre> <p>Actionlistener:</p> <pre><code>@Override public void actionPerformed(ActionEvent e) { if(e.getSource()==buttonOne){ soundName = "messageOne.wav"; }else if(e.getSource()==buttonTwo){ soundName = "messageTwo.wav"; }else if(e.getSource()==buttonThree){ soundName = "messgaeThree.wav"; }else if(e.getSource()==buttonFour){ soundName = "messageFour.wav"; } try { audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile()); } catch (UnsupportedAudioFileException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } try { clip = AudioSystem.getClip(); } catch (LineUnavailableException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } try { clip.open(audioInputStream); } catch (LineUnavailableException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); } clip.start(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload