In this post we'll discuss about playing music files using java . cool
huh ? Well, this post contains three ways to play music files.To play
files with au or wav extensions we'll use libraries already available
with JDK.For MP3 files there is use of an external API called
JLayer(Javazoom project). Ok .. let's get started.
1.sun.audio package
Import following to your class :
import sun.audio.*;
import java.io.*;
Following is the sample code :
To stop following :
AudioPlayer.player.stop(as);
2. java.applet package
import following
import java.applet.*;
Sample code :
//key.mp3 bundled with the JAR file
3.JLayer API to play mp31.sun.audio package
Import following to your class :
import sun.audio.*;
import java.io.*;
Following is the sample code :
try {
InputStream in = new FileInputStream(MusicFileName.wav); //InputStream for the audio file
AudioStream as = new AudioStream(in); // AudioStream object using InputStream of audio file
//Following is to continuously play audio until it is not stopped
AudioData data = as.getData();
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
AudioPlayer.player.play (cas); // To play continuously
AudioPlayer.player.stop (cas); // To stop
}catch (Exception ex){
System.out.println("Exception while playing audio "+ex.getMessage());
}
To play the audio without loop following can be usedInputStream in = new FileInputStream(MusicFileName.wav); //InputStream for the audio file
AudioStream as = new AudioStream(in); // AudioStream object using InputStream of audio file
//Following is to continuously play audio until it is not stopped
AudioData data = as.getData();
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
AudioPlayer.player.play (cas); // To play continuously
AudioPlayer.player.stop (cas); // To stop
}catch (Exception ex){
System.out.println("Exception while playing audio "+ex.getMessage());
}
AudioPlayer.player.start(as);
To stop following :
AudioPlayer.player.stop(as);
2. java.applet package
import following
import java.applet.*;
Sample code :
//key.mp3 bundled with the JAR file
AudioClip ac = Applet.newAudioClip(ClassName.class.getClassLoader().getResource("key.mp3"));
ac.play(); //play once
ac.stop(); //stop playing
ac.loop(); //play continuously
ac.play(); //play once
ac.stop(); //stop playing
ac.loop(); //play continuously
Download the JLayer library .
import following :
import javazoom.jl.player.Player;
import java.io.FileInputStream;
Sample code :
try
{
FileInputStream mp3_file=new FileInputStream("key.mp3");
Player mp3=new Player(mp3_file);
mp3.play();
}
catch(Exception e)
{
System.out.println("Error while playing audio "+e.getMessage());
}
Ok.. that's not all.There is one little problem.
While playing this, any other code won't run.Which means your app will freeze until music stops playing.
Following is the enhanced code that uses Thread to make it work.
public class Player implements Runnable{
Thread musicThread = new Thread(this);
AdvancedPlayer mp3Player;
public void loadMusic(){
try{
mp3Player = new AdvancedPlayer(PlayerView.class.getClassLoader().getResourceAsStream(filename.mp3)); //input stream of mp3 file bundled in JAR file
} catch (JavaLayerException ex){
System.out.println("Exception while loading file "+ex);
} catch (FileNotFoundException ex) {
System.out.println("Audio file not found "+ex);
}
}
public void playMusic(){
musicThread.start();
}
public void run() {
try {
mp3Player.play();
} catch (JavaLayerException ex) {
System.out.println("Error while playing audio "+ ex);
}
}
}
Thread musicThread = new Thread(this);
AdvancedPlayer mp3Player;
public void loadMusic(){
try{
mp3Player = new AdvancedPlayer(PlayerView.class.getClassLoader().getResourceAsStream(filename.mp3)); //input stream of mp3 file bundled in JAR file
} catch (JavaLayerException ex){
System.out.println("Exception while loading file "+ex);
} catch (FileNotFoundException ex) {
System.out.println("Audio file not found "+ex);
}
}
public void playMusic(){
musicThread.start();
}
public void run() {
try {
mp3Player.play();
} catch (JavaLayerException ex) {
System.out.println("Error while playing audio "+ ex);
}
}
}
No comments:
Post a Comment
Thank you for your comment