www.pudn.com > Play.rar > Play.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.media.*;
import javax.media.bean.playerbean.*;
public class Play
{
public static void main(String[] args)
{
PlayerFrame aPlayerFrame = new PlayerFrame();
aPlayerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aPlayerFrame.setVisible(true);
}
}
class PlayerFrame extends JFrame
{
public PlayerFrame()
{
setTitle("Play a music");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add the menu and the Open and Exit menu items
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new OpenAction());
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
aMediaPlayer.setPlaybackLoop(true);
aMediaPlayer.setControlPanelVisible(true);
add(aMediaPlayer);
}
void DealFile(String FileName)
{
try
{
File sourceFile = new File(FileName);
if (aPlayer == null)
{
aPlayer = Manager.createRealizedPlayer(sourceFile.toURI().toURL());
aMediaPlayer.setPlayer(aPlayer);
}
else
aMediaPlayer.setDataSource(Manager.createDataSource(sourceFile.toURI().toURL()));
pack();
aMediaPlayer.start();
}
catch(NoPlayerException e)
{
MyErrorDialog errorDialog = new MyErrorDialog(PlayerFrame.this,"File format");
errorDialog.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
MyErrorDialog errorDialog = new MyErrorDialog(PlayerFrame.this,"Bad File Name");
errorDialog.setVisible(true);
}
}
private class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JFileChooser chooser = new JFileChooser();
if(lastPath == null)
chooser.setCurrentDirectory(new File("."));
else
chooser.setCurrentDirectory(lastPath);
chooser.addChoosableFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".au") || f.isDirectory();
}
public String getDescription()
{
return "sun media(*.au)";
}
});
chooser.addChoosableFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".wav") || f.isDirectory();
}
public String getDescription()
{
return "sound wave(*.wav)";
}
});
chooser.addChoosableFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".mp3") ||
f.getName().toLowerCase().endsWith(".mp2") ||
f.getName().toLowerCase().endsWith(".mp1") ||
f.isDirectory();
}
public String getDescription()
{
return "mpeg media(*.mp1, *.mp2, *.mp3)";
}
});
if (chooser.showOpenDialog(PlayerFrame.this) == JFileChooser.APPROVE_OPTION)
{
name = chooser.getSelectedFile().getPath();
lastPath = chooser.getCurrentDirectory();
DealFile(name);
}
}
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
private String name;
private File lastPath;
private MediaPlayer aMediaPlayer = new MediaPlayer();
private Player aPlayer;
}
class MyErrorDialog extends JDialog
{
MyErrorDialog(Frame ownFrame, String message)
{
super(ownFrame, "Error occorred", true);
setLocationRelativeTo(ownFrame);
JPanel aPanel = new JPanel();
JLabel aLabel = new JLabel(message+" error");
aPanel.add(aLabel);
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
add(aPanel, BorderLayout.CENTER);
add(ok, BorderLayout.SOUTH);
pack();
}
}