How to play MP3 file in Applet – JMF
In previous article, you learn about how to play audio file in applet, however, normal JDK sound API does not supports to play MP3 file, you need to download and install the Java Media Framework (JMF) to play MP3 file in Applet.
Tutorials
In this tutorial, you will create a simple Applet JMF player, which allow to play MP3 file.
1. Download Java Media Framework (JMF)
Go JMF website to download it and install in your computer.
2. Download JMF MP3 plugin
JMF is not support MP3 file by default, you need to install this JMF MP3 plugin
3. Create a Applet player
This is a Applet player using Java Media Framework (JMF) to play MP3 file.
package com.mkyong.applet; import java.applet.Applet; import java.awt.Button; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.media.ControllerEvent; import javax.media.ControllerListener; import javax.media.Manager; import javax.media.Player; public class LoadSoundApplet extends Applet implements ActionListener, ControllerListener { Button play, stop; private Player player; private static final String PLAY = "PLAY"; private static final String STOP = "STOP"; public void init(){ play = new Button(); play.setLabel(PLAY); play.setActionCommand(PLAY); play.addActionListener(this); add(play); stop = new Button(); stop.setLabel(STOP); stop.setActionCommand(STOP); stop.addActionListener(this); add(stop); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(PLAY)){ try{ player = Manager.createPlayer(new URL(getCodeBase(),"BillyJean.mp3")); player.addControllerListener(this); player.start(); } catch(Exception ex){ ex.printStackTrace(); } }else if(e.getActionCommand().equals(STOP)){ player.stop(); }else{ player.stop(); } } @Override public void controllerUpdate(ControllerEvent c) { // TODO Auto-generated method stub if(player == null) return; } }
4. Create a HTML
Create a HTML to include the Applet.
<html> <head><title>Testing JMF</title></head> <body> <h1>Applet Play MP3 - Java Media Framework (JMF)</h1> <applet width=300 height=100 code="com.mkyong.applet.LoadSoundApplet.class"> </applet> </body> </html>
5. Output
After you clicked on the Play button, Micheal Jackson – Billy Jean will start to play ~

Reference
1. JMF Official site
2. JMF FAQ
3. Official SimpleAppletPlayer Example
4. Dzone JMF player example
- Java Core Technology - Java RegEx, Java XML, Java I/O, Java Misc
- J2EE Frameworks - Hibernate, Spring 2.5, Spring MVC, Struts 1.x, Struts 2.x
- Build Tools - Maven, Archiva
- Unit Test - jUnit, TestNG
- Client Scripts - jQuery