Você está na página 1de 3

import java.awt.

*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;

public class MusicPlayer extends JFrame


{
File file;
Player p;
Component cont;
Container c;
Component visual;
MusicPlayer()
{
super("Muzic Player");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);

JMenuBar menubar= new JMenuBar();

JMenu menu= new JMenu("Menu");


menu.setMnemonic(KeyEvent.VK_M);

JMenuItem open= new JMenuItem("Choose file");


open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{openfile();
startplay();
}
}
);
open.setMnemonic(KeyEvent.VK_F);

JMenuItem exit=new JMenuItem("Exit");


exit.setMnemonic(KeyEvent.VK_E);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{System.exit(0);}
}
);

menu.add(open);
menu.add(exit);
menubar.add(menu);
setJMenuBar(menubar);

public void openfile()


{
JFileChooser filech= new JFileChooser();

int result=filech.showOpenDialog(this);

if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = filech.getSelectedFile();
}

public void startplay()


{
if (file==null)
return;
removepreviousplayer();
try
{
p=Manager.createPlayer(file.toURL());
p.addControllerListener(new ControllerListener()
{
public void controllerUpdate(ControllerEvent ce)
{
if(ce instanceof RealizeCompleteEvent)
{
c = getContentPane();
cont= p.getControlPanelComponent();
visual=p.getVisualComponent();
if(visual!=null)
c.add(visual,BorderLayout.CENTER);
if ( cont != null )
c.add(cont,BorderLayout.SOUTH);
c.doLayout();
}
}
});
p.start();
}

catch(Exception e)
{
JOptionPane.showMessageDialog( this, "Invalid file or location", "Error
loading file", JOptionPane.ERROR_MESSAGE );

}
}
public void removepreviousplayer()
{
if ( p == null )
return;

p.stop();
p.close();
if ( visual != null )
c.remove( visual );

if ( cont != null )
c.remove( cont );
}

public static void main(String args[])


{
MusicPlayer mp= new MusicPlayer();
mp.addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
});
}
}

Você também pode gostar