Friday, April 9, 2010

Testing with Java Media Framework

I did a small experiment with Java Media Framework. Following tutorial was very helpful in my experiment - http://www.scribd.com/doc/6552759/Jmf-Tutorial

I created a simple audio player with JMF. For this, I basically used the Manager class and the Player interface in JMF library.

Given below is the source code of the simple program I wrote for audio player example.

import javax.media.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;

public class SimpleAudioPlayer {

private Player audioPlayer = null;

public SimpleAudioPlayer(URL url) throws IOException, NoPlayerException,
CannotRealizeException{
audioPlayer = Manager.createRealizedPlayer(url);
}
public SimpleAudioPlayer(File file) throws IOException,
NoPlayerException,
CannotRealizeException{
this(file.toURL());
}
public void play(){
audioPlayer.start();
}
public void stop(){
audioPlayer.stop();
audioPlayer.close();
}

public static void main(String args[])
{
File audioFile = new File(args[0]);
try {
SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile);
player.play();
try {
Thread.sleep(10000); //play the song for 10 seconds
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.stop(); // stop the player
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

* Here the Manager class acts as the factory for creating many of the interface types exposed in JMF.

* We create the Player instance using this Manager class


I tested this code with the command-line interface. You can pass any audio file supported by JMF. I tested it with MP3 and WAV.

No comments:

Post a Comment