How to Play MP3 in Java
Today we will have a look at the ways to play MP3 in Java. The native libraries of Java do not allow us to play audio that is in MP3 format thus here we use two external libraries that demand minimal setup.
Play an MP3 File Using JLayer
Below, we create a class called MP3Player
and two variables in which the first is the String that will hold the name and path of the MP3 file to play, and the second variable is the object jlPlayer
of the class Player
. Then we generate a constructor of the class MP3Player
and take the mp3FileToPlay
String as a parameter and initialize the class variable.
We make a play()
method that takes the mp3FileToPlay
and pass it to the constructor of FileInputStream
that converts the file to an InputStream
and then we pass this InputStream
object to BufferedInputStream
that converts it into a BufferedInputStream
. Now we pass the bufferedInputStream
object to the constructor of the Player
class that returns an object.
One thing to note is that the thread in which the audio is playing that thread gets blocked until the audio processing is completed. We do not want to block the main thread, so we create a new Thread()
and call the run()
method inside it. In the run()
method, we call the play()
function that comes with the Player
class of the library. To start the thread, we call the start()
function of the thread.
Now in the main()
method, we initialize filename
with the MP3 filename and then create an object of the MP3Player
class with the filename
passed in its constructor. To start the audio, we call mp3Player.play()
. After that, we get the Scanner
object and take input from the user if the input is equal to stop
, then we call the mp3Player.close()
that calls the close()
method of the Player
class, and it stops the music.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
import javazoom.jl.player.Player;
public class PlayMP3 {
public static void main(String[] args) {
String filename = "example.mp3";
MP3Player mp3Player = new MP3Player(filename);
mp3Player.play();
Scanner sc = new Scanner(System.in);
System.out.println("Write stop to stop the music: ");
if (sc.nextLine().equalsIgnoreCase("stop")) {
mp3Player.close();
}
}
}
class MP3Player {
private final String mp3FileToPlay;
private Player jlPlayer;
public MP3Player(String mp3FileToPlay) {
this.mp3FileToPlay = mp3FileToPlay;
}
public void play() {
try {
FileInputStream fileInputStream = new FileInputStream(mp3FileToPlay);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
jlPlayer = new Player(bufferedInputStream);
} catch (Exception e) {
System.out.println("Problem playing mp3 file " + mp3FileToPlay);
System.out.println(e.getMessage());
}
new Thread() {
public void run() {
try {
jlPlayer.play();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
}
public void close() {
if (jlPlayer != null)
jlPlayer.close();
}
}
Play an MP3 Using JACo MP3Player
JACo MP3Player
is an external library that we have to include in our project to use. This is a very tiny library and easy to integrate. In the following example, we create an object of the MP3Player
class that is a class from the library. We pass a File
object in the constructor of MP3Player
. We call the play()
function from the MP3Player
class to start the audio.
To stop the audio processing, we first check if the audio has stopped using the isStopped()
method, and if it’s stopped, we call Thread.sleep()
that pauses the thread for some time.
import jaco.mp3.player.MP3Player;
import java.io.File;
public class PlayMP3 {
public static void main(String[] args) {
try {
File f = new File("example.mp3");
MP3Player mp3Player = new MP3Player(f);
mp3Player.play();
while (!mp3Player.isStopped()) {
Thread.sleep(5000);
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn