2 Replies Latest reply: Apr 8, 2009 1:04 AM by 577611 RSS

    Playing an audio file(.wav) using Java

    800705
      Dear friends,
      I searched the net and found some code to play a .wav file using Java. Here's the code snippet :
      public static void main(String[] args) throws IOException, InterruptedException {
                     String fileName = "C://FillTimesheet.wav";
                     InputStream in = null;
           
                     try {
                          in = new FileInputStream(fileName);
                
      
                     } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          System.out.println("Media file not present in C drive.");
                          
                          e.printStackTrace();
                     }
           
                     AudioStream as = null;
                     try {
                          as = new AudioStream(in);
                     } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                     }
           
                     AudioPlayer.player.start(as);
                     Thread.sleep(5000);
                  
           
        }
      The input audio file is named as FillTimesheet.wav which i recorded and then placed it in C:. The program works fine. But when i modify it to make it look the following:
      public static void main(String[] args) throws IOException, InterruptedException {
                     String fileName = "C://FillTimesheet.wav";
                     InputStream in = null;
           
                     try {
                          in = new FileInputStream(fileName);
                
      
                     } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          System.out.println("Media file not present in C drive.");
                          
                          e.printStackTrace();
                     }
           
                     AudioStream as = null;
                     try {
                          as = new AudioStream(in);
                     } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                     }
                                           for(int i=0;i<=9;i++){
                          AudioPlayer.player.start(as);
                          Thread.sleep(5000);
                                                      }
                  
           
        }
      to make it play the file 10 times, it plays it for once only. Whereas if i modify the program to make it look like the following:
      public static void main(String[] args) throws IOException, InterruptedException {
                     String fileName = "C://FillTimesheet.wav";
                     InputStream in = null;
           
                     try {
                          in = new FileInputStream(fileName);
                
      
                     } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          System.out.println("Media file not present in C drive.");
                          
                          e.printStackTrace();
                     }
           
                     AudioStream as = null;
                     try {
                          as = new AudioStream(in);
                     } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                     }
           
                     AudioPlayer.player.start(as);
                     Thread.sleep(5000);
                  
           
        }
      The input audio file is named as FillTimesheet.wav which i recorded and then placed it in C:. The program works fine. But when i modify it to make it look the following:
      public static void main(String[] args) throws IOException, InterruptedException {
                for(int i =0;i<=9;i++){
                                                      String fileName = "C://FillTimesheet.wav";
                     InputStream in = null;
           
                     try {
                          in = new FileInputStream(fileName);
                
      
                     } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          System.out.println("Media file not present in C drive.");
                          
                          e.printStackTrace();
                     }
           
                     AudioStream as = null;
                     try {
                          as = new AudioStream(in);
                     } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                     }
                                           for(int i=0;i<=9;i++){
                          AudioPlayer.player.start(as);
                          Thread.sleep(5000);
                                                      }
                        
                      }
                  
           
        }
      it plays the file 10 times. Why is it so ? Kindly do let me know the reason behind it.

      Thanks in advance,
      Subhash