Loading and playing multiple .wav files in java

269 views Asked by At

I am working on a research project and in the project I need to input 8 .wav files (sorted into an array in a random order) and have them play when a button is pressed. Only the first file is playing and it only plays when the first button is clicked. Here is the relevant section of code (I know I can create the buttons with a loop, but I don't for various reasons):

 /**creates the buttons
   * **/
 public void createButtons() {

   /**sets the files**/
   intervals[intervalOrder[0]] = new File("0.wav");
   intervals[intervalOrder[1]] = new File("1.wav");
   intervals[intervalOrder[2]] = new File("2.wav");
   intervals[intervalOrder[3]] = new File("3.wav");
   intervals[intervalOrder[4]] = new File("4.wav");
   intervals[intervalOrder[5]] = new File("5.wav");
   intervals[intervalOrder[6]] = new File("6.wav");
   intervals[intervalOrder[7]] = new File("7.wav");
   /**creates each button individually **/

   buttons[0] = new JButton("Play");
   buttons[0].addActionListener(new ActionListener() { //actionListenerPlays     the appropriate interval
     public void actionPerformed(ActionEvent arg0) { 
       File yourFile = intervals[0];
   try {
     /**AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
     **/
     Clip clip = AudioSystem.getClip();
     clip.open(AudioSystem.getAudioInputStream(yourFile));
     clip.start();
     Thread.sleep(clip.getMicrosecondLength() / 10000);
     clip.close();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[1] = new JButton("Play");
   buttons[1].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) { 
       File yourFile = intervals[1];
       try {
     /**AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream); **/
     Clip clip = AudioSystem.getClip();
     clip.open(AudioSystem.getAudioInputStream(yourFile));
     clip.start();
     Thread.sleep(clip.getMicrosecondLength() / 10000);
     clip.close();
   }
   catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[2] = new JButton("Play");
   buttons[2].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) { 
       File yourFile = intervals[2];
       try {
     AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[3] = new JButton("Play");
   buttons[3].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) { 
       File yourFile = intervals[3];
       try {
     AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[4] = new JButton("Play");
   buttons[4].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) { 
       File yourFile = intervals[4];
       try {
     AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[5] = new JButton("Play");
   buttons[5].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) {
       File yourFile = intervals[5];
       try {
     AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[6] = new JButton("Play");
   buttons[6].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) {
       File yourFile = intervals[6];
       try {
     AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
   buttons[7] = new JButton("Play");
   buttons[7].addActionListener(new ActionListener() { //actionListenerPlays the appropriate interval
     public void actionPerformed(ActionEvent arg0) {
       File yourFile = intervals[7];
       try {
    /** AudioInputStream stream;
     AudioFormat format;
     DataLine.Info info;
     Clip clip;        
     stream = AudioSystem.getAudioInputStream(yourFile);
     format = stream.getFormat();
     info = new DataLine.Info(Clip.class, format);
     clip = (Clip) AudioSystem.getLine(info);
     clip.open(stream);
     clip.start();**/
     Clip clip = AudioSystem.getClip();
     clip.open(AudioSystem.getAudioInputStream(yourFile));
     clip.start();
   }
       catch (Exception e) {
     //exceptions TBD
   }
 }
   });
 }
0

There are 0 answers