How to play MP3 file through MP3 SPI and JavaSound ?
Make sure that JLayer, Tritonus and MP3SPI librairies are available in your CLASSPATH. It means that you have jl1.0.1.jar, tritonus_share.jar, mp3spi1.9.5.jar in your CLASSPATH. Write a class, that relies on JavaSound API, that plays audio file (see sample below).
import javax.sound.sampled.*;
import java.io.*;
[...]
public void testPlay(String filename)
{
try {
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputStream(file);
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)
{
//Handle exception.
}
}
private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
byte[] data = new byte[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();
}
}
private 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;
}
[...] |
How to read audio properties ?
Only J2SE 1.5 will allow to pass audio properties. However, MP3SPI provides a workaround (class cast) to get these audio properties for J2SE 1.3 and 1.4.
TAudioFileFormat => duration, title, author, album, date, comment, copyright, mp3.framerate.fps, mp3.length.frames, mp3.vbr.scale, mp3.id3tag.v2 .... See javazoom.spi.mpeg.sampled.file.MpegAudioFileFormat javadoc to learn more.
TAudioFormat => vbr, bitrate. See javazoom.spi.mpeg.sampled.file.MpegAudioFormat javadoc to learn more. Here is a sample below :
import org.tritonus.share.sampled.TAudioFormat;
import org.tritonus.share.sampled.file.TAudioFileFormat;
[...]
File file = new File(filename);
AudioFileFormat baseFileFormat = null;
AudioFormat baseFormat = null;
baseFileFormat = AudioSystem.getAudioFileFormat(file);
baseFormat = baseFileFormat.getFormat();
// TAudioFileFormat properties
if (baseFileFormat instanceof TAudioFileFormat)
{
Map properties = ((TAudioFileFormat)baseFileFormat).properties();
String key = "author";
String val = (String) properties.get(key);
key = "mp3.id3tag.v2";
InputStream tag= (InputStream) properties.get(key);
}
// TAudioFormat properties
if (baseFormat instanceof TAudioFormat)
{
Map properties = ((TAudioFormat)baseFormat).properties();
String key = "bitrate";
Integer val = (Integer) properties.get(key);
}
[...] |
How to use the MP3 equalizer ?
Equalizer is an array of 32 floats. Each float value could be in [-1.0 +1.0]. Here is a sample below :
[...]
din = AudioSystem.getAudioInputStream(decodedFormat, in);
// DecodedMpegAudioInputStream properties
if (din instanceof javazoom.spi.PropertiesContainer)
{
Map properties = ((javazoom.spi.PropertiesContainer) din).properties();
float[] equalizer = (float[]) properties.get("mp3.equalizer");
equalizer[0] = 0.5;
equalizer[31] = 0.25;
}
[...] |
Where is the list of all available properties ?
Check out Javadoc or readme.txt to find out all updated properties. Here is an extract of AudioFileFormat properties :
Standard parameters : - duration : [Long], duration in microseconds. - title : [String], Title of the stream. - author : [String], Name of the artist of the stream. - album : [String], Name of the album of the stream. - date : [String], The date (year) of the recording or release of the stream. - copyright : [String], Copyright message of the stream. - comment : [String], Comment of the stream. Extended MP3 parameters : - mp3.version.mpeg : [String], mpeg version : 1,2 or 2.5 - mp3.version.layer : [String], layer version 1, 2 or 3 - mp3.version.encoding : [String], mpeg encoding : MPEG1, MPEG2-LSF, MPEG2.5-LSF - mp3.channels : [Integer], number of channels 1 : mono, 2 : stereo. - mp3.frequency.hz : [Integer], sampling rate in hz. - mp3.bitrate.nominal.bps : [Integer], nominal bitrate in bps. - mp3.length.bytes : [Integer], length in bytes. - mp3.length.frames : [Integer], length in frames. - mp3.framesize.bytes : [Integer], framesize of the first frame. framesize is not constant for VBR streams. - mp3.framerate.fps : [Float], framerate in frames per seconds. - mp3.header.pos : [Integer], position of first audio header (or ID3v2 size). - mp3.vbr : [Boolean], vbr flag. - mp3.vbr.scale : [Integer], vbr scale. - mp3.crc : [Boolean], crc flag. - mp3.original : [Boolean], original flag. - mp3.copyright : [Boolean], copyright flag. - mp3.padding : [Boolean], padding flag. - mp3.mode : [Integer], mode 0:STEREO 1:JOINT_STEREO 2:DUAL_CHANNEL 3:SINGLE_CHANNEL - mp3.id3tag.genre : [String], ID3 tag (v1 or v2) genre. - mp3.id3tag.track : [String], ID3 tag (v1 or v2) track info. - mp3.id3tag.v2 : [InputStream], ID3v2 frames. - mp3.shoutcast.metadata.key : [String], Shoutcast meta key with matching value. For instance : mp3.shoutcast.metadata.icy-irc=#shoutcast mp3.shoutcast.metadata.icy-metaint=8192 mp3.shoutcast.metadata.icy-genre=Trance Techno Dance mp3.shoutcast.metadata.icy-url=http://www.di.fm |
|