Java Code To Play MP3 File

An example code on playing not only mp3 but any audio file in Java in just 2 statements. Your first step to create an audio player.

Install Java Media Framework


For this you'll need to install Java Media Framework (JMF) in your PC. This is much easier than coding, it's just like you install other softwares. Clicking on Agree, Next, Finish will do the thing. You will also not have any head ache with the classpath settings because it is automatically done. If you know how to install software, then you probably will install the JMF. The download link is provided above, all you need to do is to accept the license agreement first and then if choose your Operating System and then click on the file. If you use windows, i recommend to download Windows Performance Pack

Let's Play




import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
public static void main(String args[]) throws Exception
{


// Take the path of the audio file from command line
File f=new File(args[0]);


// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());


// Start the music
p.start();


// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);


// Read a line and store it in st
String st=s.nextLine();


// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}


Explanation


Player is an interface in the javax.media package. As you cannot create an object for an interface directly some class implementing it is written and it's object is used. Manager is such a class that does the thing.

There are several states in which a Player will be. They are

UNREALIZED: Meaning, the player knows nothing about what it has to do (about the media)

REALIZING: Meaning, the player is realizing it's world (determining resources) necessary to do the job (playing the file)

REALIZED: Meaning, the player gets its resources to play the file and will also have some info about the type of the media etc.

PREFETCHING: It's getting ready now, it is knowing how could it play the file.

PREFETCHED: Now, it knows how to play the media. It can fire now.

START: Start the music, starts playing the file.

We have created a realized player, it does not mean that the UNREALIZED, REALIZING steps are skipped instead they are automatically done. The method createRealizedPlayer(URL url) takes a URL which points to the media file. For getting the URL object from a File object, you can used toURL() method in the java.io.File class, as it is deprecated we've called toURI() method which returns java.net.URI object and then the toURL() method in it.The user also needs to stop the music, so input is taken from the user.

Output however cannot be shown, you'll have to try it, remember that when executing the program give the complete path of the audio file otherwise ArrayIndexOutOfBounds exception will arise. The command might look like this


java AudioPlay "C:\Whistle.mp3"

No comments: