Home > Java > Simple MP3 Player dengan JMF (Java Media Framework)

Simple MP3 Player dengan JMF (Java Media Framework)


JMF, ya JMF (Java Media Framework) yaitu library Java yang berhubungan dengan media baik audio maupun video serta kemampuan lainya yang dimiliki JMF. Sederhananya, saya akan berbagi tentang penggunaan JMF dalam sebuah mp3 player mini.  Baiklah, langsung saja.

1. Download dulu library JMF dari web resminya. Yang paling simpel nya adalah mendownload file exe nya, karena kita tidak perlu lagi mengcopy library ke direktori JRE kita. Dengan file exe tadi, bisa langsung di-instal tuh jmf nya..

link dari website resmi : http://www.oracle.com/technetwork/java/javase/download-142937.html

Langsung download file exe nya disini : jmf2.1 windows x86

2. Library jmf.jar harus ada di direktori( file itu harus ada di kedua direktori ini). Jika kita menggunakan file exe tadi, akan ada 2 file otomatis sudah diletakkan di kedua direktori ini (jmf.jar dan sound.jar).

a. C:\Program Files\Java\jdk1.7.0\jre\lib\ext\ —> untuk proses compile

b. C:\Program Files\Java\jre7\lib\ext\   –> untuk proses running

3.  List Program JMFMp3Player.java

/*
* Created by Pizaini
* pizaini.wordpress.com
* zheiner.taurus@gmail.com
* (c) 2011
* Go Open Source
* file : JMFMp3Player.java
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

/**
*
* @author Pizaini
*/
public class JMFMp3Player extends JFrame{
   private JButton buttonBrowse, buttonPlay;
   private JLabel labelDuration;
   private JTextField textFileName;
   private JPanel panel1, panel2, panel3;
   private Player player;
   private final String title = "JMF MP3 Player";

   public JMFMp3Player() {
      initComponents();
      setLabelDuration(0D);
   }

   private void initComponents(){
      setSize(new Dimension(400, 200));
      setResizable(false);
      setTitle(title);
      setLocationRelativeTo(null);
      setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      buttonBrowse = new JButton();
      buttonBrowse.setText("...");
      buttonBrowse.setPreferredSize(new Dimension(50, 30));
      buttonBrowse.setToolTipText("Browse Media");
      buttonBrowse.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            openFileDialog();
         }
      });

      buttonPlay = new JButton();
      buttonPlay.setText("Play");
      buttonPlay.setToolTipText("Play Media");
      buttonPlay.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            playMedia();
         }
      });

      textFileName = new JTextField();
      textFileName.setPreferredSize(new Dimension(300, 30));
      textFileName.setEditable(false);
      textFileName.setToolTipText("File Name");

      labelDuration = new JLabel();
      labelDuration.setHorizontalAlignment(SwingConstants.CENTER);
      labelDuration.setText("Duration :");
      labelDuration.setToolTipText("Duration");

      panel1 = new JPanel();
      panel1.add(textFileName);
      panel1.add(buttonBrowse);

      panel2 = new JPanel();
      panel2.add(buttonPlay);

      add(panel1, BorderLayout.NORTH);
      add(panel2, BorderLayout.CENTER);
      add(labelDuration, BorderLayout.SOUTH);
      pack();
   }

   private void setLabelDuration(Double duration){
      Integer d = duration.intValue();
      if(d > 0){
         Integer minutes = d / 60;
         Integer second = d % 60;
         labelDuration.setText("Duration  "+minutes+" : "+second);
      }
   }

   private void openFileDialog() {
      FileDialog fileDialog = new FileDialog(this, "Open Media File", FileDialog.LOAD);
      fileDialog.setVisible(true);
      try {
         if(fileDialog.getFile() != null){
            File file = new File(fileDialog.getDirectory(), fileDialog.getFile());
            textFileName.setText(file.getName());
            try{
               player = Manager.createRealizedPlayer(file.toURI().toURL());
               Double time = player.getDuration().getSeconds();
               setLabelDuration(time);
            }catch(NoPlayerException noPx){}
         }
      } catch (IOException ex) {
      } catch (CannotRealizeException ex) {
      }
   }

   private void playMedia() {
   // TODO add your handling code here:
   if(buttonPlay.getText().equals("Play")){
      try{
         player.start();
         buttonPlay.setText("Pause");
         buttonBrowse.setEnabled(false);
         buttonPlay.setToolTipText("Pause Playing");
         this.setTitle(title + " [ Playing ... ]");
      }catch(Exception ex){
      }
      }else if(buttonPlay.getText().equals("Pause")){
         try{
            player.stop();
            buttonPlay.setText("Play");
            buttonBrowse.setEnabled(true);
            buttonPlay.setToolTipText("Play Media");
            this.setTitle(title + " [ Pause ]");
       }catch(Exception ex){}
       }
   }
   //running
   public static void main(String[] args){
      try {
         UIManager.setLookAndFeel(new javax.swing.plaf.nimbus.NimbusLookAndFeel());
      } catch (UnsupportedLookAndFeelException ex) {
      }
      EventQueue.invokeLater(new Runnable() {
          public void run() {
             new JMFMp3Player().setVisible(true);
          }
      });
   }
}

Complie source code di atas :

Dan…..go running…..

Button Browse sebagai File Dialog untuk memilih file media (mp3) yang akan di play. Setelah file dipilih player ini otomatis mendeteksi durasi lamanya file akan dimainkan. Dan tombol play untuk memainkan file yang telah dipilih.

Selamat mencoba, semoga berhasil dan bermanfaat dengan posting yang sederhana ini…

  1. 18 January 2015 at 15:11

    gan, gimana cara mengatur supaya label durasinya berjalan mundur hingga sampai nol bersamaan saat lagunya habis? “kayak di aplikasi musik gitu gan”

  2. 27 June 2013 at 23:28

    Gan, ane mau tanya .. cara masukin List Program (Source Code) itu ke dalam postingan gmna??
    soalnya ane juga mau berbagi … mohon bantuannya …

    • 27 June 2013 at 23:30

      List Program (Source Code) mkstnya gan,

      • 29 June 2013 at 13:27

        gunakan ini gan:

        
        //letakkan source code di sini
        
        
  3. dher
    3 June 2012 at 22:15

    thanks tutorialnya bro..

    • 4 June 2012 at 12:46

      sama2 gan..semoga bermanfaat…

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: