Java - Explain how to play audio in a stand alone application

Explain how to play audio in a stand alone application

import java.io.File; import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class PlayAudio
{
   private static final int EXTERNAL_BUFFER_SIZE = 128000;
   public static void main(String[] args)
   {
       String file1 = args[0];
       File file2 = new File(file1);
       AudioInputStream ais = null;
       try
       {
           ais = AudioSystem.getAudioInputStream(file2);
       }
       catch (Exception e)
       {
           e.printStackTrace();
           System.exit(1);
       }
       AudioFormat af = ais.getFormat();
       SourceDataLine sdf = null;
       DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af);
       try
       {
           sdf = (SourceDataLine) AudioSystem.getLine(dli);
           sdf.open(af);
       }
       catch (LineUnavailableException e)
       {
           e.printStackTrace();
           System.exit(1);
       }
       catch (Exception e)
       {
           e.printStackTrace();
           System.exit(1);
       }
       sdf.start();

       int dat = 0;
       byte[] data1 = new byte[EXTERNAL_BUFFER_SIZE];
       while (dat != -1)
       {
           try
           {
               dat = ais.read(data1, 0, data1.length);
           }
           catch (IOException e)
           {
               e.printStackTrace();
           }
           if (dat >= 0)
           {
               int nBytesWritten = sdf.write(data1, 0, dat);
           }
       }
       sdf.drain();
       sdf.close();
       System.exit(0);
   }
}
Java - What is the difference between an Applet and an Application?
Difference between an Applet and an Application - An applet runs with the control of a browser, where as an application runs standalone...
What are the component and container classes?
Component and container classes - A component is a graphical object. A few examples of components are: Button, Canvas, Checkbox...
Purpose of invalidate and validate methods
Purpose of invalidate and validate methods - The invalidate method is called as a side efftect of an addition or deleting some component...
Post your comment