/* * AudioPlayer02GUI.java * * Created on May 31, 2005, 9:04 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package juke.rbaldwin; /*File AudioPlayer02GUI.java Copyright 2003 Richard G. Baldwin Demonstrates playback of an audio file. The path and name of the audio file is specified by the user in a text field. A GUI appears on the screen containing the following components: Text field for the file name Play Stop After entering an audio file name in the text field, the user can click the Play button to cause the program to play the audio file. By default, the program will play the entire file and then get ready to play another file, or to play the same file again. If the user clicks the Stop button while the file is being played, playback will terminate. However, because the audio data is buffered in a large buffer in the playback loop, there may be a noticeable delay between the time that the Stop button is clicked and the time that the playback actually terminates. The text field contains the default audio file name, junk.au, when the GUI first appears on the screen. The program displays the format of the audio data in the file before playing the file. The format is displayed on the command- line screen. Tested using SDK 1.4.1 under Win2000 ************************************************/ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.sound.sampled.*; public class AudioPlayer02GUI extends JFrame{ AudioFormat audioFormat; AudioInputStream audioInputStream; SourceDataLine sourceDataLine; boolean stopPlayback = false; final JButton pauseBtn = new JButton("Pause"); final JButton playBtn = new JButton("Play"); final JButton openBtn = new JButton("Open"); final JButton resumeBtn = new JButton("Resume"); final JTextField textField = new JTextField("Press the Open Button to load an mp3, wav or speex file."); private String strFilename; public static void main(String args[]){ new AudioPlayer02GUI(); }//end main //-------------------------------------------// public AudioPlayer02GUI(){//constructor pauseBtn.setEnabled(false); playBtn.setEnabled(true); //Instantiate and register action listeners // on the Play and Stop buttons. playBtn.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e){ pauseBtn.setEnabled(true); playBtn.setEnabled(false); playAudio();//Play the file }//end actionPerformed }//end ActionListener );//end addActionListener() pauseBtn.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e){ //Terminate playback before EOF // stopPlayback = true; sourceDataLine.stop(); }//end actionPerformed }//end ActionListener );//end addActionListener() /** resumeBtn.addActionListener( new ActionListener(){ public void actionPerformed( ActionEvent e){ //Terminate playback before EOF // stopPlayback = true; sourceDataLine.start(); }//end actionPerformed }//end ActionListener );//end addActionListener() */ getContentPane().add(playBtn,"West"); getContentPane().add(openBtn,"Center"); ButtonHandler handler = new ButtonHandler(); openBtn.addActionListener( handler ); getContentPane().add(pauseBtn,"East"); getContentPane().add(resumeBtn, "South"); resumeBtn.addActionListener( handler ); getContentPane().add(textField,"North"); setTitle("Copyright 2003, R.G.Baldwin"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(320,100); setVisible(true); }//end constructor //-------------------------------------------// private String openFile() { /////////////////////////// JFileChooser fileChooser = new JFileChooser(); // fileChooser.setMultiSelectionEnabled(true); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY); int result = fileChooser.showOpenDialog( this ); if(result == JFileChooser.APPROVE_OPTION) { JOptionPane.showMessageDialog( null, "You pressed selected OKAY, the name is: " + fileChooser.getSelectedFile().getName() ); //!?!? figure out how to return getFILES w/multipole files selected... } else if ( result == JFileChooser.CANCEL_OPTION) { //!???!fix this shit return; } String fileNamer = fileChooser.getSelectedFile().getAbsolutePath(); // String fileName2URl = fileNamer.toURL(); return strFilename = fileNamer; ///////// } private class ButtonHandler implements ActionListener { // handle button event public void actionPerformed( ActionEvent event ) { if (event.getSource() == openBtn ) openFile(); else if (event.getSource() == resumeBtn ) resumeFile(); } } //This method plays back audio data from an // audio file whose name is specified in the // text field. private void playAudio() { try{ //File soundFile = new File(textField.getText()); File soundFile = new File (strFilename); textField.setText(strFilename); audioInputStream = AudioSystem.getAudioInputStream(soundFile); //audioFormat = audioInputStream.getFormat(); AudioFormat sourceFormat = audioInputStream.getFormat(); //jsr style System.out.println(audioFormat); AudioFormat.Encoding targetEncoding = AudioFormat.Encoding.PCM_SIGNED; audioInputStream = AudioSystem.getAudioInputStream(targetEncoding, audioInputStream); audioFormat = audioInputStream.getFormat(); //put these here so we can see the SOURCE AudioFormat info, which may vary w/each file //and the OUTPUT AudioFormat, which is the the same w/each file System.out.println(sourceFormat); System.out.println(audioFormat); DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat); sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo); //Create a thread to play back the data and // start it running. It will run until the // end of file, or the Stop button is // clicked, whichever occurs first. // Because of the data buffers involved, // there will normally be a delay between // the click on the Stop button and the // actual termination of playback. //this is w/out thread attempt: //this is commented, because trying w/out thread new PlayThread().start(); }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end playAudio private void resumeFile() { sourceDataLine.start(); } //=============================================// //Inner class to play back the data from the // audio file. class PlayThread extends Thread{ byte tempBuffer[] = new byte[10000]; public void run(){ try{ sourceDataLine.open(audioFormat); sourceDataLine.start(); int cnt; //Keep looping until the input read method // returns -1 for empty stream or the // user clicks the Stop button causing // stopPlayback to switch from false to // true. while((cnt = audioInputStream.read( tempBuffer,0,tempBuffer.length)) != -1 && stopPlayback == false){ if(cnt > 0){ //Write data to the internal buffer of // the data line where it will be // delivered to the speaker. sourceDataLine.write( //Data written to the SourceDataLine object is automatically //delivered to the speaker on the computer. tempBuffer, 0, cnt); }//end if }//end while //Block and wait for internal buffer of the // data line to empty. sourceDataLine.drain(); sourceDataLine.close(); //Prepare to playback another file pauseBtn.setEnabled(false); playBtn.setEnabled(true); stopPlayback = false; }catch (Exception e) { e.printStackTrace(); System.exit(0); }//end catch }//end run }//end inner class PlayThread //===================================// }//end outer class AudioPlayer02GUI.java