We all listen to songs daily in our PC,majority of them or all of them are mp3 formats.We have different kinds of Media Players in Internet.But,regardless of all those applications if we use a Media Player made with Java,how it will be?


Sounds Intresting right?


Two years ago when I am searching through java websites I came across this intresting application called Java MP3 Player.Even though its not very userfriendly,its a very good application which is developed using basic Swing capabilities. 


                                                  
You can see the source code here :
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mediaplayer;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javazoom.jlgui.basicplayer.*;
/**
 *
 * @author Administrator
 */
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(new FlowLayout(FlowLayout.LEFT,3,3));

        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.");
    }
}

This is the JavaMP3Player class.The following code is the Main class.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mediaplayer;

import javax.swing.*;

/**
 *
 * @author Tutorial Jinni
 */
class Main {

    /**
     * @param args the command line arguments
     */
    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("Song-location.mp3");
    }
}


You can execute this first by grouping both these classes in a single folder,compiling the Main class and try executing it.This application mainly usef JMF(Java Media Framework) which is used to create Players and several other media related applications.


In this,we used BasicPlayer class and applied some techniques of Swing!


You can download the program full source code here .To execute this full source code you need NetBeans Developer IDE.


We will see more Java Applications in my next posts!!


Hope you liked it!


Stay Tuned!!