How to Play Music
Step 1. Download this source code INTO IT'S OWN CLASS called MP3.
package blah2;
/*************************************************************************
* Compilation: javac -classpath .:jl1.0.jar MP3.java (OS X)
* javac -classpath .;jl1.0.jar MP3.java (Windows)
* Execution: java -classpath .:jl1.0.jar MP3 filename.mp3 (OS X / Linux)
* java -classpath .;jl1.0.jar MP3 filename.mp3 (Windows)
*
* Plays an MP3 file using the JLayer MP3 library.
*
* Reference: http://www.javazoom.net/javalayer/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
*
* and put it in your working directory with this file MP3.java.
*
*************************************************************************/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 {
private String filename;
private Player player;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}
public void close() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
// test client
public static void main(String[] args) {
String filename = "OopsSmall.mp3";
MP3 mp3 = new MP3(filename);
mp3.play();
// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);
}
}
System.out.println(sum);
// when the computation is done, stop playing it
mp3.close();
// play from the beginning
mp3 = new MP3(filename);
mp3.play();
}
}
Step 2
Download the JLayer library at this link: http://www.javazoom.net/javalayer/sources.html
Step 3
Unzip the downloaded archive. Remember where it is!
Step 4
Link the downloaded archive (.jar file) to your project in Eclipse
package blah2;
/*************************************************************************
* Compilation: javac -classpath .:jl1.0.jar MP3.java (OS X)
* javac -classpath .;jl1.0.jar MP3.java (Windows)
* Execution: java -classpath .:jl1.0.jar MP3 filename.mp3 (OS X / Linux)
* java -classpath .;jl1.0.jar MP3 filename.mp3 (Windows)
*
* Plays an MP3 file using the JLayer MP3 library.
*
* Reference: http://www.javazoom.net/javalayer/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
*
* and put it in your working directory with this file MP3.java.
*
*************************************************************************/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 {
private String filename;
private Player player;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}
public void close() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
// test client
public static void main(String[] args) {
String filename = "OopsSmall.mp3";
MP3 mp3 = new MP3(filename);
mp3.play();
// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);
}
}
System.out.println(sum);
// when the computation is done, stop playing it
mp3.close();
// play from the beginning
mp3 = new MP3(filename);
mp3.play();
}
}
Step 2
Download the JLayer library at this link: http://www.javazoom.net/javalayer/sources.html
Step 3
Unzip the downloaded archive. Remember where it is!
Step 4
Link the downloaded archive (.jar file) to your project in Eclipse
- Open Eclipse.
- Right-click on your project folder (should have a folder icon with a 'J')
- Select Build Path, then select Add External Archives...
- Browse to the downloaded, unzipped archive.
- Click on the JLayer folder, then click on the JLayer folder inside that folder.
- You should see somthing like the file below. Select the file and the errors with 'Player' in your MP3 class should go away.
Step 5
Now the fun part.
- Select a song (.mp3 filetype) that you want to play in the background.
- Save it to your desktop.
- Drag that song into Eclipse and drop it ON YOUR PROJECT NAME. The song should appear in the package explorer as a file at the bottom of the project. IF YOU PUT IT IN THE WRONG SPOT, IT WILL NOT PLAY, NOR WILL IT GIVE YOU AN ERROR
Step 6
Insert the following code into your program.
At the top, at the class level, create an MP3 object. Like this: MP3 mp3 = new MP3("OopsSmall.mp3");
Step 7
Insert the following code into your program where you want to start playing the music: mp3.play();
My Music Doesn't Play
This could be caused by a couple of problems:
- You do not have your mp3 file in the project folder. Maybe you accidentally put it in the package folder or the src folder.
- You have the name of the file wrong. Remember case matters, so if the name of the file is "OopsSmall.mp3" and in your Java program you typed in "OopsSmall.MP3", java will not be able to find it. Make sure the names match EXACTLY!