Skip to main content

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"

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.