Sunday, August 15, 2010

Java Media Player ( MP3 Player )

Recently i was in a situation when i have to play mp3 in my java project, i google a bit and quickly found a solution, a pure java library namely JLayer by javazoom . i found i very interesting as it provide almost all functionality i wanted expect that i want a GUI for playing and pausing the media, much like a basic media player so i develop a small media player and i to share it as someone else might want it, so here is the code of the java media player.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javazoom.jlgui.basicplayer.*;

class JavaMediaPlayer extends JPanel implements BasicPlayerListener{

private JProgressBar jpb;
private JButton play;
private BasicPlayer player=new BasicPlayer();;
private String imagePath="images/";
private static int p=0;
private static boolean playBoolean=true;

JavaMediaPlayer(){

setLayout(null);

player.addBasicPlayerListener(this);

play=new JButton(new ImageIcon(imagePath+"pause.png"));
play.setToolTipText("pause");play.setBounds(2,2,20,20);

play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(playBoolean){
pausePlaying();
play.setToolTipText("Play");
play.setIcon(new ImageIcon(imagePath+"play.png"));
playBoolean=false;
return;
}
if(playBoolean==false){
resumePlaying();
play.setToolTipText("Pause");
play.setIcon(new ImageIcon(imagePath+"pause.png"));
playBoolean=true;
return;
}
}
});

jpb=new JProgressBar();
jpb.setBorderPainted(true);
jpb.setBounds(25,2,280,20);

add(play);
add(jpb);
}

public void startPlaying(String fileName){
try{
play.setIcon(new ImageIcon(imagePath+"pause.png"));
play.setToolTipText("Pause");
playBoolean=true;
player.open(new File(fileName));
player.play();//actually play file
}
catch(Exception d){
System.out.println(d);
}
}
private void pausePlaying(){
try{
player.pause();
}
catch(Exception e){
System.out.println(e);
}
}
private void stopPlaying(){
try{
player.stop();
jpb.setValue(0);
}
catch(Exception d){
System.out.println(d);
}
}
private void resumePlaying(){
try{
player.resume();
}
catch(Exception d){
System.out.println(d);
}
}

public void opened(Object arg0, Map map) {
String k=map.get("audio.length.frames").toString();
jpb.setMaximum(Integer.parseInt(k)-15);
}

public void progress(int arg0, long arg1, byte[] arg2, Map map) {
String s=map.get("mp3.frame").toString();
int k=(p-(Integer.parseInt(s))*(-1));
jpb.setValue(k);
}

public void stateUpdated(BasicPlayerEvent bpev) {
if(bpev.getCode()==BasicPlayerEvent.STOPPED){
// Something here
}
}
public void setController(BasicController arg0) {
// throw new UnsupportedOperationException("Not supported yet.");
}
}


and the initializing code of the above code for java media player is as follows you can use it as you want it, its just for demonstration.

import javax.swing.*;

class Main {

public static void main(String[] args) {
JFrame f=new JFrame();
JavaMediaPlayer jmp=new JavaMediaPlayer();
f.add(jmp);
f.setDefaultCloseOperation(3);
f.setVisible(true);
f.setSize(330,60);
f.setTitle("Java Media Player - Tutorial Jinni");
jmp.startPlaying("PATH_TO_MP3_FILE");
}
}


after you run it should be somewhat like this.

java media player

You can download a netbeans project, it contains all the libraries you want to make a java mp3 player.

download free java mp3 player

Download Sample Code

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems