import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; /** * Play a *.wav or *.au file. * * @author Knute Johnson */ public class Play { /** * Play a *.wav or *.au file * * @param args args[0] on command line is name of file to play */ public static void main ( String[] args ) { try { AudioInputStream ais = AudioSystem.getAudioInputStream ( new File ( args[0] ) ); AudioFormat af = ais.getFormat(); DataLine.Info info = new DataLine.Info ( SourceDataLine.class, af ); if ( ! AudioSystem.isLineSupported ( info ) ) { System.out.println( "unsupported line" ); System.exit( 0 ); } int frameRate = (int)af.getFrameRate(); System.out.println( "Frame Rate: " + frameRate ); int frameSize = af.getFrameSize(); System.out.println( "Frame Size: " + frameSize ); int bufSize = frameRate * frameSize / 10; System.out.println( "Buffer Size: " + bufSize ); SourceDataLine line = (SourceDataLine) AudioSystem.getLine( info ); line.open( af, bufSize ); line.start(); byte[] data = new byte[bufSize]; int bytesRead; while ( ( bytesRead = ais.read( data, 0, data.length ) ) != -1 ) line.write( data, 0, bytesRead ); line.drain(); line.stop(); line.close(); } catch ( Exception e ) { System.out.println( e.toString() ); } System.exit( 0 ); } }