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 |