rendered paste bodyimport 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 StuckPlayLearn extends JFrame{ private static final int EXTERNAL_BUFFER_SIZE = 128000; private JButton openBtn, stopBtn, playBtn, resumeBtn; private Container container; private BorderLayout layout; private FlowLayout layout2; String strFilename; SourceDataLine line, liner; PlayThread pT = new PlayThread(); AudioInputStream audioInputStream, derivedAudioInputStream; /** some of this code is from jsresources.org *their comments ripped to keep simplicity until program functions * * 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 StuckPlayLearn() { 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"); resumeBtn = new JButton("Resume"); openBtn.setEnabled(true); stopBtn.setEnabled(true); playBtn.setEnabled(true); resumeBtn.setEnabled(true); ButtonHandler handler = new ButtonHandler(); openBtn.addActionListener( handler ); //openBtn.addActionListener(this); playBtn.addActionListener( handler ); stopBtn.addActionListener( handler ); resumeBtn.addActionListener( handler ); container.add(openBtn, BorderLayout.NORTH); container.add(playBtn, BorderLayout.CENTER); container.add(stopBtn, BorderLayout.PAGE_END); container.add(resumeBtn, BorderLayout.EAST); setSize(250,170); setVisible(true); setDefaultCloseOperation( EXIT_ON_CLOSE);}public static void main(String[] args) { StuckPlayLearn spL = new StuckPlayLearn(); }///////// 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(); else if ( event.getSource() == resumeBtn) resumeFile(); } } ///////// 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() { /////////////////////////////////////////////////////////////////// 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) { System.out.println("### 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", // try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } line.start(); System.out.println("line 1st time: " + line); ///////////////////////////////////////////////////////////////////now introduce thread for actual playback: //line.start(); //PlayThread pT = new PlayThread(); pT.start(); //new PlayThread().start(); playBtn.setEnabled(false); // return liner = line; }private void stopFile( /*SourceDataLine liner */){ pT.stop(); playBtn.setEnabled(true);}private void resumeFile(){ //new AudioInputStream(originalAIS, originalAIS.getFormat(), length); pT.resume(); playBtn.setEnabled(false); }////////////////////////////class PlayThread implements Runnable{ Thread pbThread; // pTRunning = true; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; //byte tempBuffer[] = new byte[10000]; public void start() { pbThread = new Thread(this); pbThread.setName("PlaybackMonitor"); pbThread.start(); line.start(); } public void stop() { line.stop(); } public void resume() { System.out.println("line to resume: " + line); line.start(); } ///////////////////////////////////////////////////////////////////////////////// public void run() { pTRunning = true; System.out.println("audioInputStream: " + audioInputStream); System.out.println("line 2nd time: " + line); try{ int cnt; int nBytesRead = 0; //Keep looping until the input // read method returns -1 for // empty stream. 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); } } line.drain(); line.close(); }catch (Exception e) { System.out.println(e); System.exit(0); }//end catch } //////////////////////////////////////////////////////////////////////////////////////////////////////////// }////////////////////////////end inner class playthread }