package juke.mp3.problems; import javax.sound.sampled.*; import java.io.*; import java.net.*; import java.io.IOException; public class AudioPlayer implements /*Ap_Interface,*/Runnable { AudioFormat format; AudioInputStream audioInputStream; SourceDataLine line; Object fileReference; byte[] abData = new byte[128000]; Thread newPter; private static boolean DEBUG = true; public AudioPlayer() { fileReference = null; audioInputStream = null; line = null; } public void loadAudioClipLess(File file) throws Exception{ fileReference = file; initAudioInputStream(); } //decide which method to pass to: public void initAudioInputStream() throws UnsupportedAudioFileException, LineUnavailableException, IOException { if (fileReference instanceof File) { initAudioInputStream((File) fileReference); } } public void initAudioInputStream(File file) throws UnsupportedAudioFileException, IOException { audioInputStream = AudioSystem.getAudioInputStream(file); } public void initLine(/*File file*/) throws Exception { // System.out.println("now we'll see if file is passed: " + file); createLine(); openLine(); } private void createLine() throws LineUnavailableException { if (line != null) { return; } /* * From the AudioInputStream, i.e. from the sound file, we * fetch information about the format of the audio data. These * information include the sampling frequency, the number of * channels and the size of the samples. There information * are needed to ask Java Sound for a suitable output line * for this audio file. */ //Here i made it so if you hve the sun.com mp3 plugin, it'll read mp3s... AudioFormat sourceFormat = audioInputStream.getFormat(); AudioFormat.Encoding targetEncoding = AudioFormat.Encoding.PCM_SIGNED; audioInputStream = AudioSystem.getAudioInputStream(targetEncoding, audioInputStream); AudioFormat audioFormat = audioInputStream.getFormat(); if (DEBUG) { System.out.println("BaseAudioStream.initLine(): audio format: " + audioFormat); } /* * Asking for a line is a rather tricky thing. * ... * Furthermore, we have to give Java Sound a hint about how * big the internal buffer for the line should be. Here, * we say AudioSystem.NOT_SPECIFIED, signaling that we don't * care about the exact size. Java Sound will use some default * value for the buffer size. */ DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED); line = (SourceDataLine) AudioSystem.getLine(info); System.out.println("Original line set at: " + line + "\n");//shows SOURCE eg: mp3/speex/wav/etc. 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", } private void openLine() throws LineUnavailableException { if (line == null) { return; } AudioFormat audioFormat = audioInputStream.getFormat(); line.open(audioFormat); } ///////////////////// /** * Starts playback. */ public void startPlayback() { newPter = new Thread( this, "The playback Thread"); newPter.start(); line.start(); } public void pausePlayback() { line.stop(); } public void resumePlayback() { line.start(); } public void stopPlayback() { line.stop(); try { audioInputStream.close(); } catch (IOException e) { System.out.println("Cannot close stream" + e); } /* if (line != null) { line.flush(); line.stop(); } synchronized (audioInputStream) { closeStream(); } */ } protected void closeStream() { // Close stream. try { if (audioInputStream != null) { audioInputStream.close(); System.out.println("Stream closed"); } } catch (IOException e) { System.out.println("Cannot close stream" + e); } } public void run() { System.out.println("run was called "); boolean 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 AudioPlayer.java } /////End AudioPlayer.java