All pastes #13156 Raw Edit

Anonymous

public java v1 · immutable
#13156 ·published 2005-06-02 05:40 UTC
rendered paste body
/* * DecodingAudioPlayerGUI.java * * Created on May 29, 2005, 2:58 PM */package juke.mp3;/* *	DecodingAudioPlayerGUI.java * *	This file is part of jsresources.org *//* * Copyright (c) 1999, 2000 by Matthias Pfisterer * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright notice, *   this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright *   notice, this list of conditions and the following disclaimer in the *   documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. *//*|<---            this code is formatted to fit into 80 columns             --->|*/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 DecodingAudioPlayerGUI 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 DecodingAudioPlayerGUI() {    super("GUI for Decoding Audio...we shall see...");        container = getContentPane();        layout = new BorderLayout();        //layout2 = new FlowLayout();        //try  setTitle("Capture/Playback Demo");         //in place of super        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)	{        DecodingAudioPlayerGUI dapGui = new DecodingAudioPlayerGUI();                  	}    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;                    }                   /**                    File currentDir = new File("C:/j2sdk1.4.2_04/5netbeans/Juke/build/classes/");//new File(System.getProperty("user.dir"));    try    {      currentDirURL = currentDir.toURL();        *////////// //changing here to reflect the stuff from MAIN                    //from the original DecodingAudioPlayer.java file                   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() == playBtn)     playFile( strFilename );          else if ( event.getSource() == stopBtn)     stopFile( liner );                     }   }     private SourceDataLine playFile( String strFilename ){                        File file = new File (strFilename);    //File 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");//                System.out.println("audioFormat: " + audioFormat + "\n");////javasojava 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);			}		}                                playBtn.setEnabled(false);		// line.close();  //i put the !?!??!! following line here, believe it will                 //close resources..hoping? guessing?..!?!?!  //the PLAY button stayed gray/program locked as before/commenting out                //line.stop();                    return liner = line;    }private void stopFile( SourceDataLine liner ){        			liner.stop();		        }	private static void out(String strMessage)	{		System.out.println(strMessage);	}	}/*** DecodingAudioPlayerGUI.java ***/