Importing mp3 files
Author |
Message |
elmocookies
|
Posted: Wed Jun 11, 2008 9:34 pm Post subject: Importing mp3 files |
|
|
Does anyone know how to import mp3 files onto Java and then play them?
Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Jun 12, 2008 9:29 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Light
![](http://compsci.ca/v3/uploads/user_avatars/100128.jpg)
|
|
|
|
![](images/spacer.gif) |
Hendo
![](http://compsci.ca/v3/uploads/user_avatars/1049240745485a9912c2ba0.jpg)
|
Posted: Sun Jun 15, 2008 7:08 pm Post subject: 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)
JAVA: | 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 [] buf = new byte [line.getBufferSize ()];
while ((numRead = stream.read (buf, 0, buf.length)) >= 0)
{
int offset = 0;
while (offset < numRead)
{
offset += line.write (buf, offset, numRead - offset);
}
}
line.drain ();
line.stop ();
}
catch (Exception e)
{
e.printStackTrace ();
System.exit (1);
}
}
public static void delay (int ms)
{
try
{
Thread.sleep (ms);
}
catch (Exception e)
{
;
}
}
|
and then you can simply play a song by calling it like this:
code: | playLine("mysong.wav") |
This next snippet is for smaller clips like loops or short music clips (under 30 seconds long)
JAVA: |
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
code: | playClip("myloop.wav",true) |
Hope that helps |
|
|
|
|
![](images/spacer.gif) |
|
|