在java中加载和播放多个.wav文件

时间:2015-06-11 20:04:52

标签: java audio jbutton wav

我正在研究一个研究项目,在项目中我需要输入8个.wav文件(按随机顺序排列成一个数组),并在按下按钮时播放它们。仅播放第一个文件,仅在单击第一个按钮时播放。 这是相关的代码部分(我知道我可以用循环创建按钮,但我不是出于各种原因):

 /**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 个答案:

没有答案