public static void main (String [] args) { Audiotest AT = new new Audiotest ( "my tests, this must be the letter of the audio file" ); at.start (); }
Import java.io.File; Import java.io.IOException; Import javax.sound.sampled.AudioFormat The; 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; Import javax.sound.sampled.UnsupportedAudioFileException; public class Audiotest the extends the Thread { // 1. audio file defined variables, the variables needed: one for storing the audio file name of the object String object filename Private String filename; //2. constructor initializes filename public Audiotest (String filename) { the this .FileName = filename; } @Override public void RUN () { // 1. defines a file object reference, that points to a file called filename File = a sourceFile new new File (filename); // define a AudioInputStream input for receiving audio data AudioInputStream AudioInputStream = null ; // use AudioSystem get audio input of the audio stream the try { AudioInputStream = AudioSystem.getAudioInputStream (a sourceFile); } the catch (an UnsupportedAudioFileException E) { e.printStackTrace () ; }the catch (IOException E) { e.printStackTrace (); } // . 4, to obtain a AudioFormat AudioInputStream format AudioFormat the format = AudioInputStream.getFormat (); // 5. The data source can write data line SoureDataLine line data SourceDataLine = auline null ; // Get audio format supported by the data line DataLine.Info the DataLine.Info info = new new the DataLine.Info (SourceDataLine a. class , the format); // get match a specified row info type the try { auline = ( SourceDataLine a) AudioSystem.getLine (info); // open line with the specified format, causing the line to acquire any required system resources and become operational auline.open (); } the catch (A LineUnavailableException E) { // the TODO Auto-Generated Block the catch e.printStackTrace (); } // allow an execution of a data row of data I / O auline.start (); // write data int nBytesRead = 0 ; byte [] = abData new new byte [2 ]; // read from a specified maximum number of audio streams data bytes, and into the given byte array. the try { the while (nBytesRead = -1! ) { nBytesRead = AudioInputStream.read (abData, 0 , abData.length); //This line of data is written by the source data mixer IF (nBytesRead> = 0 ) auline.write (abData, 0 , nBytesRead); } } the catch (IOException E) { e.printStackTrace (); } the finally { auline.drain ( ); auline.close (); }
} }