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);
}
}