The recent software engineering has done a Java version of the media player, I put the code has been a fine shrink, hey, from thousands of lines of code reduced to more than 250 lines:), but also a lot less function oh, no list, no file filtering and so on. But though small, spite Oh, play mp3, MPG, MPEG, AVI and so on music files no problem oh. It's a lite version, hehe. Here's the code.
Package edu.whu.bbflyerwww.mymusic;
Import java.awt.BorderLayout;
Import java.awt.Component;
Import java.awt.Dimension;
Import Java.awt.Toolkit;
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.RealizeCompleteEvent;
Import javax.media.PrefetchCompleteEvent;
Import Javax.media.bean.playerbean.MediaPlayer;
Import Javax.swing.Icon;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JPopupMenu;
Import Javax.swing.JPanel;
Import Javax.swing.JOptionPane;
/**
* Compact Media Player MyMusic (Java implementation) <br>
* Implement <br> based on JMF (Java Media Framework)
* No very complex features, no very nice interface, just entertainment <br>
* @environment Eclipse
* @author bbflyerwww
* @email bbflyer@qq.com
* @free_version
*/
public class MyMusic extends JFrame implements Controllerlistener {
MediaPlayer player; Media Player
JPanel Center; Getcontentpane () visual components, put V_part
JPanel South; Getcontentpane () control component, put C_part
Component V_part;
Component C_part;
JFileChooser Chooser; File selection
/**
* Constructor
*/
Public MyMusic () {
Initialize ();
This.setvisible (TRUE);
}
/**
* App Initialization
*/
private void Initialize () {
This.settitle ("MyMusic");
This.setdefaultcloseoperation (Jframe.exit_on_close);
Initjmenubar ();
Initjcontentpane ();
UpdateUI ();
Initcomponents ();
}
/**
* Initialize menu layout and events
*/
private void Initjmenubar () {
Jpopupmenu.setdefaultlightweightpopupenabled (FALSE);
Final JMenu file = new JMenu ("file");
Final JMenuItem FileOpen = new JMenuItem ("Open local Music ...");
Final JMenuItem appexit = new JMenuItem ("Exit");
File.add (FileOpen);
File.addseparator ();
File.add (AppExit);
Fileopen.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Cmdopenmusic ();
}
});
Appexit.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Cmdappexit ();
}
});
Final JMenu help = new JMenu ("help");
Final JMenuItem about = new JMenuItem ("About ...");
Help.add (about);
About.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent e) {
Cmdappabout ();
}
});
Final JMenuBar bar = new JMenuBar ();
Bar.add (file);
Bar.add (Help);
This.setjmenubar (bar);
}
/**
* Initialization of ContentPane
*/
private void Initjcontentpane () {
This.getcontentpane (). setlayout (New BorderLayout ());
Center = new JPanel ();
south = new JPanel ();
Center.setlayout (New BorderLayout ());
South.setlayout (New BorderLayout ());
This.getcontentpane (). Add (center, borderlayout.center);
This.getcontentpane (). Add (South, Borderlayout.south);
Setjcenterbackground ();
}
/**
* Initialize setting form background
*/
private void Setjcenterbackground () {
Final String picture = "edu//whu//bbflyerwww//image//. jpg in maple";
Final icon icon = new ImageIcon (picture);
Final JLabel label = new JLabel (icon);
Center.add (label, Borderlayout.center);
}
/**
* Update settings The screen location where the form is displayed
*/
private void Updatejlocation () {
Dimension SCR = Toolkit.getdefaulttoolkit (). Getscreensize ();
Dimension fsize = This.getsize ();
if (Fsize.height > Scr.height)
Fsize.height = Scr.height;
if (Fsize.width > Scr.width)
Fsize.width = Scr.width;
This.setlocation ((scr.width-fsize.width)/2,
(scr.height-fsize.height)/2);
}
/**
* Update the entire window interface
*/
private void UpdateUI () {
This.pack ();
This.validate ();
Updatejlocation ();
}
/**
* Initialization of other components
*/
private void Initcomponents () {
Player = new MediaPlayer ();
Player.addcontrollerlistener (this);
Chooser = new JFileChooser ();
}
/**
* Releasing Resources
*/
private void Close () {
This.dispose ();
if (player!= null) {
Player.close ();
}
}
/**
* About
*/
private void Cmdappabout () {
Joptionpane.showmessagedialog (This,
"@author: Bbflyerwww",
"About",
Joptionpane.plain_message);
}
/**
* Exit System
*/
private void Cmdappexit () {
Close ();
System.exit (0);
}
/**
* Open Local media file dialog box
*/
private void Cmdopenmusic () {
int value = Chooser.showopendialog (this);
if (value = = jfilechooser.approve_option) {
String filename = Chooser.getselectedfile (). toString ();
Play (filename);
}
}
/**
* Implementation Playback
* @param MediaName
*/
private void Play (String MediaName) {
try {
URL mediaurl = new URL ("File:" + MediaName);
Play (Mediaurl);
catch (Exception e) {
System.out.println ("The file you entered has a problem");
E.printstacktrace ();
}
}
/**
* Implementation play
* @param URL
* @throws Exception
*/
private void pla Y (url url) throws Exception {
player.setmedialocation (url.tostring ());
player.realize ();
}
/**
* Implementing Controllerlistener Interface
* @param event
*/
public void Controllerupdate (Controllerevent event) {
if (event instanceof realizecompleteevent) {
if (V_part!= null)
center.remove (v_part);
if ((V_part = Player.getvisualcomponent ())!= null)
center.add (V_part, Borderlayout.center);
else
setjcenterbackground ();
if (C_part!= null)
south.remove (c_part);
if ((C_part = Player.getcontrolpanelcomponent ())!= null)
south.add (c_ part, Borderlayout.south);
player.prefetch ();
}
if (Event instanceof prefetchcompleteevent) {
updateui ();
player.start ();
}
}
public static void Main (String args[]) {
New MyMusic ();
}
}
Here are a few pictures to catch, hehe