
-----------------------------------
elmocookies
Wed Jun 11, 2008 9:34 pm

Importing mp3 files
-----------------------------------
Does anyone know how to import mp3 files onto Java and then play them?

Thanks.

-----------------------------------
DemonWasp
Thu Jun 12, 2008 9:29 pm

RE:Importing mp3 files
-----------------------------------
Go look up the Java Media Framework (javax.media.*). Google that and you should find all the API you'll need. I've never used it myself, so I can't be of specific help, but Google should find you a tutorial just fine.

-----------------------------------
Light
Fri Jun 13, 2008 9:25 am

RE:Importing mp3 files
-----------------------------------
You can get the JMF (Java media framework) with MP3 plug-in here: http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html

And find documentation about it here: http://java.sun.com/javase/technologies/desktop/media/jmf/reference/docs/index.html

-----------------------------------
Hendo
Sun Jun 15, 2008 7:08 pm

Re: Importing mp3 files
-----------------------------------
Heres a couple neat methods I put together for playing wav files (im not sure if you have to use mp3 but this will only work with wavs)

This one is used if the wav is fairly long (usually full songs, anything longer than say 30 seconds and you should use this)
import javax.sound.sampled.*;
 public void playLine (String file)
    {
	try
	{
	    AudioInputStream stream = AudioSystem.getAudioInputStream (new File (file));

	    // From URL
	    //stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));

	    // At present, ALAW and ULAW encodings must be converted
	    // to PCM_SIGNED before it can be played
	    AudioFormat format = stream.getFormat ();
	    if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
	    {
		format = new AudioFormat (
			AudioFormat.Encoding.PCM_SIGNED,
			format.getSampleRate (),
			format.getSampleSizeInBits () * 2,
			format.getChannels (),
			format.getFrameSize () * 2,
			format.getFrameRate (),
			true);        // big endian
		stream = AudioSystem.getAudioInputStream (format, stream);
	    }

	    // Create line
	    SourceDataLine.Info info = new DataLine.Info (
		    SourceDataLine.class, stream.getFormat (),
		    ((int) stream.getFrameLength () * format.getFrameSize ()));
	    SourceDataLine line = (SourceDataLine) AudioSystem.getLine (info);
	    line.open (stream.getFormat ());
	    line.start ();

	    // Continuously read and play chunks of audio
	    int numRead = 0;
	    byte 

and then you can simply play a song by calling it like this:
playLine("mysong.wav")

This next snippet is for smaller clips like loops or short music clips (under 30 seconds long)

import javax.sound.sampled.*; public void playClip (String file, boolean loop)
    {
	try
	{
	    // From file
	    AudioInputStream stream = AudioSystem.getAudioInputStream (new File (file));

	    // From URL
	    // stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));

	    // At present, ALAW and ULAW encodings must be converted
	    // to PCM_SIGNED before it can be played
	    AudioFormat format = stream.getFormat ();
	    if (format.getEncoding () != AudioFormat.Encoding.PCM_SIGNED)
	    {
		format = new AudioFormat (
			AudioFormat.Encoding.PCM_SIGNED,
			format.getSampleRate (),
			format.getSampleSizeInBits () * 2,
			format.getChannels (),
			format.getFrameSize () * 2,
			format.getFrameRate (),
			true);        // big endian
		stream = AudioSystem.getAudioInputStream (format, stream);
	    }

	    // Create the clip
	    DataLine.Info info = new DataLine.Info (
		    Clip.class, stream.getFormat (), ((int) stream.getFrameLength () * format.getFrameSize ()));
	    Clip clip = (Clip) AudioSystem.getLine (info);

	    // This method does not return until the audio file is completely loaded
	    clip.open (stream);

	    // Start playing
	    if (loop)
		clip.loop (Clip.LOOP_CONTINUOUSLY);
	    else
		clip.start ();
	}
	catch (MalformedURLException e)
	{
	}
	catch (IOException e)
	{
	}
	catch (LineUnavailableException e)
	{
	}
	catch (UnsupportedAudioFileException e)
	{
	}
    }


Using clips gives you the option of looping it and I added a handy loop boolean in the parameters so if you wanted to play a clip and loop it you would call it like this
playClip("myloop.wav",true)


Hope that helps
