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.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.DataLine; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.sound.sampled.*; public class StuckPlay2 extends JFrame { private static final int EXTERNAL_BUFFER_SIZE = 128000; private JButton openBtn, stopBtn, playBtn; /* final JButton stopBtn = new JButton("Pause"); final JButton playBtn = new JButton("Play"); final JButton openBtn = new JButton("Open"); final JButton resumeBtn = new JButton("Resume"); */ private Container container; private BorderLayout layout; private FlowLayout layout2; private String strFilename; private SourceDataLine line, liner; PlayThread pT = new PlayThread(); AudioInputStream audioInputStream; /** * This variable is used to distinguish stopped state from * paused state. In case of paused state, m_bRunning is still * true. In case of stopped state, it is set to false. Doing so * will terminate the thread. */ private boolean pTRunning; public StuckPlay2() { super("GUI for Decoding Audio...we shall see..."); container = getContentPane(); layout = new BorderLayout(); openBtn = new JButton("Open"); stopBtn = new JButton("Stop"); playBtn = new JButton("Playback"); openBtn.setEnabled(true); stopBtn.setEnabled(true); playBtn.setEnabled(true); ButtonHandler handler = new ButtonHandler(); openBtn.addActionListener( handler ); //openBtn.addActionListener(this); playBtn.addActionListener( handler ); stopBtn.addActionListener( handler ); container.add(openBtn, BorderLayout.NORTH); container.add(playBtn, BorderLayout.CENTER); container.add(stopBtn, BorderLayout.PAGE_END); setSize(250,170); setVisible(true); setDefaultCloseOperation( EXIT_ON_CLOSE); } public static void main(String[] args) { StuckPlay2 stuckPlay2 = new StuckPlay2(); } ///////// private class ButtonHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { if ( event.getSource() == openBtn) openFile( /*strFilename*/ ); else if ( event.getSource() == playBtn) playFile( /*strFilename*/ ); else if ( event.getSource() == stopBtn) stopFile( /*liner */); } } ///////// private String openFile() { JFileChooser fileChooser = new JFileChooser(); 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() ); } else if ( result == JFileChooser.CANCEL_OPTION) { //!???!fix this shit return; } String fileNamer = fileChooser.getSelectedFile().getAbsolutePath(); return strFilename = fileNamer; } //private SourceDataLine playFile( String strFilename ) public void playFile() { //line.start(); //PlayThread pT = new PlayThread(); pT.start(); //new PlayThread().start(); playBtn.setEnabled(false); // return liner = line; } private void stopFile( /*SourceDataLine liner */) { //liner.stop(); pT.stop(); playBtn.setEnabled(true); } //////////////////////////// class PlayThread implements Runnable { /* public PlayThread( String strFilename ) { } */ Thread pbThread; public void start() { pbThread = new Thread(this); pbThread.setName("PlaybackMonitor"); pbThread.start(); } public void run() { int nBytesRead = 0; pTRunning = true; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; File file = new File (strFilename); // byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; AudioInputStream audioInputStream = null; try { audioInputStream = AudioSystem.getAudioInputStream(file); } catch (Exception e) { e.printStackTrace(); } if (audioInputStream == null) { // out("### cannot read input file: " + strFilename); } AudioFormat sourceFormat = audioInputStream.getFormat(); AudioFormat.Encoding targetEncoding = AudioFormat.Encoding.PCM_SIGNED; audioInputStream = AudioSystem.getAudioInputStream(targetEncoding, audioInputStream); AudioFormat audioFormat = audioInputStream.getFormat(); SourceDataLine line = null; DataLine.Info info = new DataLine.Info( SourceDataLine.class, audioFormat); System.out.println("sourceFormat: " + sourceFormat + "\n");//shows SOURCE eg: mp3/speex/wav/etc. System.out.println("audioFormat: " + audioFormat + "\n");////added this shows that while "opening and mp3", // the actual AudioFormat being played back is PCM(at least on windows) try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } line.start(); while (nBytesRead != -1) { try { nBytesRead = audioInputStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { int nBytesWritten = line.write(abData, 0, nBytesRead); } } } public void pause() { line.stop(); } public void stop() { line.stop(); } } /////////////////////////// }