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 StuckPlayButton extends JFrame{ private static final int EXTERNAL_BUFFER_SIZE = 128000; private JButton openBtn, stopBtn, playBtn; private Container container; private BorderLayout layout; private FlowLayout layout2; private String strFilename; private SourceDataLine liner; 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 ); stopBtn.addActionListener( handler ); playBtn.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 ) openFile(); 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 ){ playBtn.setEnabled(false); 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(); int nBytesRead = 0; 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); } } return liner = line; }private void stopFile( SourceDataLine liner ){ liner.stop(); } private static void out(String strMessage) { System.out.println(strMessage); } }