rendered paste bodypackage juke.mp3;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 StuckPlayButton 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; private static boolean DEBUG = true; /** * 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 StuckPlayButton() { 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) { StuckPlayButton stuckPlay = new StuckPlayButton(); }///////// private class ButtonHandler implements ActionListener { public void actionPerformed( ActionEvent event ) { /* if (event.getSource() == openBtn ) { try { openFile(); } catch (LineUnavailableException lue) { } } */ if ( event.getSource() == openBtn) try { openFile( /*strFilename*/ ); } catch (UnsupportedAudioFileException uafe) { uafe.printStackTrace(); } catch (LineUnavailableException lue) { lue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } else if ( event.getSource() == playBtn) playFile( /*strFilename*/ ); else if ( event.getSource() == stopBtn) stopFile( /*liner */); } } ///////// private void setDataSource(String name) throws UnsupportedAudioFileException, LineUnavailableException, IOException { /* m_dataSource = file; initAudioInputStream(); */ File file = new File(name); initAudioInputStream( file ); } private void initAudioInputStream( File file ) throws UnsupportedAudioFileException, IOException { audioInputStream = AudioSystem.getAudioInputStream(file); } protected void initLine() { //this is!?!?! important must do this; allows resuming i believe //will check if line exists, if not then "create()" //otherwise use existing } private void createLine() throws LineUnavailableException { AudioFormat sourceFormat = audioInputStream.getFormat(); AudioFormat.Encoding targetEncoding = AudioFormat.Encoding.PCM_SIGNED; audioInputStream = AudioSystem.getAudioInputStream(targetEncoding, audioInputStream); AudioFormat audioFormat = audioInputStream.getFormat(); 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) DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat); line = (SourceDataLine) AudioSystem.getLine(info); } private void openLine() throws LineUnavailableException { AudioFormat audioFormat = audioInputStream.getFormat(); line.open(audioFormat, line.getBufferSize()); } private String openFile() throws UnsupportedAudioFileException, LineUnavailableException, IOException { 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(); setDataSource(fileNamer); return strFilename = fileNamer; } //private SourceDataLine playFile( String strFilename )public void playFile() { //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(); //line.start(); } public void pause() { line.stop(); } public void stop() { line.stop(); } public void run() { if (DEBUG) { out("thread start"); } int nBytesRead = 0; pTRunning = true; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; // int nFrameSize = m_line.getFormat().getFrameSize(); while (nBytesRead != -1 && pTRunning) { try { nBytesRead = audioInputStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { //int nFramesToWrite = nBytesRead / nFrameSize; if (DEBUG) { out("Trying to write: " + nBytesRead); } int nBytesWritten = line.write(abData, 0, nBytesRead); if (DEBUG) { out("Written: " + nBytesWritten); } } } /* * Wait until all data are played. * This is only necessary because of the bug noted below. * (If we do not wait, we would interrupt the playback by * prematurely closing the line and exiting the VM.) */ // TODO: check how this interferes with stop() line.drain(); if (DEBUG) { out("after drain()"); } /* * Stop the line and reinitialize the AudioInputStream. * This should be done before reporting end-of-media to be * prepared if the EOM message triggers a new start(). */ stop(); if (DEBUG) { out("after this.stop()"); } /** int nBytesRead = 0; pTRunning = true; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; while (nBytesRead != -1 && pTRunning) { try { nBytesRead = audioInputStream.read(abData, 0, abData.length); } catch (IOException e) { e.printStackTrace(); } if (nBytesRead >= 0) { int nBytesWritten = line.write(abData, 0, nBytesRead); } } line.drain(); line.stop(); */ }} private static void out(String strMessage) { System.out.println(strMessage); } }