Playing an MP3

Hi folks

I'm having some trouble with my code for playing an mp3 file. This used to be working but now I've ported it over to another computer and it's not working. Maybe I'm missing a resource or maybe there's something wrong with my code.

Advice anyone?

here's the exception:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file

at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)

at tjacobs.mp3.MP3.play(MP3.java:113)

at tjacobs.mp3.MP3.main(MP3.java:173)

And here's my code:

/*

* Created on Jun 29, 2005 by @author Tom Jacobs

*

*/

package tjacobs.mp3;

import javax.sound.midi.MetaEventListener;

import javax.sound.midi.MetaMessage;

import javax.sound.midi.MidiSystem;

import javax.sound.midi.MidiUnavailableException;

import javax.sound.midi.Sequence;

import javax.sound.midi.Sequencer;

import javax.sound.sampled.*;

import javax.swing.JFileChooser;

import java.io.*;

import java.lang.reflect.Type;

/**

* Call play() with the file name of an MP3 and it will play that MP3

*/

publicclass MP3{

private MP3(){

super();

}

privatestaticclass MP3Runnableimplements Runnable{

String file;

MP3Runnable(String filename){

file = filename;

}

publicvoid run(){

play(file);

}

}

publicstaticvoid play(String filename,boolean createThread){

if (!createThread){

play(filename);

}

else{

Runnable r =new MP3Runnable(filename);

Thread t =new Thread(r);

t.start();

}

}

publicstaticvoid play(InputStream stream){

try{

AudioInputStream in= AudioSystem.getAudioInputStream(stream);

AudioInputStream din =null;

AudioFormat baseFormat = in.getFormat();

AudioFormat decodedFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,

baseFormat.getSampleRate(),

16,

baseFormat.getChannels(),

baseFormat.getChannels() * 2,

baseFormat.getSampleRate(),

false);

din = AudioSystem.getAudioInputStream(decodedFormat, in);

// Play now.

rawplay(decodedFormat, din);

in.close();

}

//catch(RuntimeException re) {}

catch (UnsupportedAudioFileException ex){

ex.printStackTrace();

}

catch (IOException iox){

iox.printStackTrace();

}

catch (LineUnavailableException lue){

lue.printStackTrace();

}

}

publicstaticvoid play(String filename)

{

File file =new File(filename);

play(file);

}

publicstaticvoid play(File audiofile){

try{

if (audiofile.getName().endsWith(".mid") || audiofile.getName().endsWith(".midi")){

Sequence sequence = MidiSystem.getSequence(audiofile );

if (sequence !=null){

// These methods may throw MidiUnavailableException

try{

final Sequencer sequencer = MidiSystem.getSequencer();

sequencer.open();

sequencer.setSequence(sequence);

sequencer.start();

MetaEventListener listener =new MetaEventListener(){

publicvoid meta(MetaMessage ev){

if ( ev.getType() == 47 ){// end of stream

//sequencer.stop();

//sequencer.close();

//System.exit( 0 );

}

}

};

}catch (MidiUnavailableException ex){

//nothing we can do

return;

}

}

return;

}

AudioInputStream in= AudioSystem.getAudioInputStream(audiofile);

AudioInputStream din =null;

AudioFormat baseFormat = in.getFormat();

AudioFormat decodedFormat =new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,

baseFormat.getSampleRate(),

16,

baseFormat.getChannels(),

baseFormat.getChannels() * 2,

baseFormat.getSampleRate(),

false);

din = AudioSystem.getAudioInputStream(decodedFormat, in);

// Play now.

rawplay(decodedFormat, din);

in.close();

}catch (Exception e)

{

e.printStackTrace();

//Handle exception.

}

}

privatestaticvoid rawplay(AudioFormat targetFormat, AudioInputStream din)throws IOException,LineUnavailableException

{

byte[] data =newbyte[4096];

SourceDataLine line = getLine(targetFormat);

if (line !=null)

{

// Start

line.start();

int nBytesRead = 0, nBytesWritten = 0;

while (nBytesRead != -1)

{

nBytesRead = din.read(data, 0, data.length);

if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);

}

// Stop

line.drain();

line.stop();

line.close();

din.close();

}

}

privatestatic SourceDataLine getLine(AudioFormat audioFormat)throws LineUnavailableException

{

SourceDataLine res =null;

DataLine.Info info =new DataLine.Info(SourceDataLine.class, audioFormat);

res = (SourceDataLine) AudioSystem.getLine(info);

res.open(audioFormat);

return res;

}

publicstaticvoid main (String args[]){

JFileChooser chooser =new JFileChooser();

File f =new File("C:/program files/englishotto/media/music");

chooser.setCurrentDirectory(f);

int result = chooser.showOpenDialog(null);

if (result == chooser.APPROVE_OPTION){

File mp3 = chooser.getSelectedFile();

//try {

play(mp3);

//FileInputStream fis = new FileInputStream(mp3);

//PushbackInputStream bis = new PushbackInputStream(fis);

//BufferedInputStream bis = new BufferedInputStream(fis);

//play(bis);

//}

//catch (FileNotFoundException ex) {

//ex.printStackTrace();

//}

}

}

}

[11173 byte] By [tjacobs01a] at [2007-10-3 6:41:47]
# 1
Can that other computer play mp3 files using any other application? (Sounds like it's missing a codec)Kaj
kajbja at 2007-7-15 1:30:56 > top of Java-index,Java Essentials,Java Programming...
# 2
> Can that other computer play mp3 files using any> other application? (Sounds like it's missing a> codec)you mean the one I'm working on now where the code doesn't work? Yeah I can play mp3's through the windows media player
tjacobs01a at 2007-7-15 1:30:56 > top of Java-index,Java Essentials,Java Programming...
# 3

> > Can that other computer play mp3 files using any

> > other application? (Sounds like it's missing a

> > codec)

>

> you mean the one I'm working on now where the code

> doesn't work? Yeah I can play mp3's through the

> windows media player

****, It was a long shot :)

kajbja at 2007-7-15 1:30:56 > top of Java-index,Java Essentials,Java Programming...
# 4

I found another way to do this that requires the JMF butwhich does work. It's included below. However when I checked out MediaLocator it requires a URL... can anyone tell me how I can change this code to use an inputstream coming from an mp3 file?

package tjacobs.mp3;

import javax.media.*;

import javax.swing.JFileChooser;

import java.io.*;

import java.net.URL;

public class MP3_2 extends Thread {

private URL url;

public MP3_2(File mp3)

{

try {

this.url = mp3.toURI().toURL();

}

catch (java.net.MalformedURLException mux) {}

}

public void run()

{

try

{

MediaLocator ml = new MediaLocator(url);

final Player player = Manager.createPlayer(ml);

player.addControllerListener(

new ControllerListener()

{

public void controllerUpdate(ControllerEvent event)

{

if (event instanceof EndOfMediaEvent)

{

player.stop();

player.close();

}

}

}

);

player.realize();

player.start();

}

catch (Exception e)

{

e.printStackTrace();

}

}

public static void main (String args[]) {

JFileChooser chooser = new JFileChooser();

File f = new File("C:/program files/englishotto/media/music");

chooser.setCurrentDirectory(f);

int result = chooser.showOpenDialog(null);

if (result == chooser.APPROVE_OPTION) {

File mp3 = chooser.getSelectedFile();

//try {

new MP3_2(mp3).start();

//FileInputStream fis = new FileInputStream(mp3);

//PushbackInputStream bis = new PushbackInputStream(fis);

//BufferedInputStream bis = new BufferedInputStream(fis);

//play(bis);

//}

//catch (FileNotFoundException ex) {

//ex.printStackTrace();

//}

}

}

}

tjacobs01a at 2007-7-15 1:30:56 > top of Java-index,Java Essentials,Java Programming...