package jmf;
import java.awt.BorderLayout;
import java.awt.CheckboxMenuItem;
import java.awt.Choice;
import java.awt.Component;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.media.ControllerClosedEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.PrefetchCompleteEvent;
import javax.media.RealizeCompleteEvent;
import javax.media.Time;
public class Test extends Frame implements ActionListener,ItemListener,ControllerListener{
private static final long serialVersionUID = -7442370271675044300L;
Player play;
String filePath = "";
Component cc,vc;
boolean loop = false;
Test(String title) {
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
public void windowClosed(WindowEvent e) {
if (play != null) {
play.close();
}
System.exit(0);
}
});
Menu menu = new Menu("文件");
MenuItem mi = new MenuItem("打开");
mi.addActionListener(this);
menu.add(mi);
CheckboxMenuItem cmi = new CheckboxMenuItem("循环");
cmi.addItemListener(this);
menu.add(cmi);
mi = new MenuItem("退出");
mi.addActionListener(this);
menu.add(mi);
MenuBar mb = new MenuBar();
mb.add(menu);
setMenuBar(mb);
setSize(300, 100);
setVisible(true);
}
public static void main(String[] args) {
new Test("媒体播放器1.0");
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("退出")){
dispose();
return;
}
FileDialog fd = new FileDialog(this,"选择播放文件",FileDialog.LOAD);
fd.setDirectory(filePath);
System.out.println(fd.getDirectory());
fd.show();
if(fd.getFile() == null){
return;
}
filePath = fd.getDirectory();
if(play!=null){
play.close();
}
try {
play = Manager.createPlayer(new MediaLocator("file:"+filePath+fd.getFile()));
} catch (NoPlayerException e1) {
System.out.println("不能创建播放器");
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
if(play == null){
System.out.println("无法创建播放器.");
return;
}
setTitle(fd.getFile());
play.addControllerListener(this);
play.prefetch();
}
public void itemStateChanged(ItemEvent e) {
loop = !loop;
}
public void controllerUpdate(ControllerEvent e) {
// TODO 自动生成方法存根
if(e instanceof ControllerClosedEvent){
if(vc!=null){
remove(vc);
}
if(cc!=null){
remove(cc);
}
}
if(e instanceof EndOfMediaEvent){
if(loop){
play.setMediaTime(new Time(0));
play.start();
}
}
if(e instanceof PrefetchCompleteEvent){
System.out.println("333");
play.start();
return;
}
if(e instanceof RealizeCompleteEvent){
vc = play.getVisualComponent();
if(vc!=null){
add(vc);
}
cc = play.getControlPanelComponent();
if(cc!=null){
add(cc,BorderLayout.SOUTH);
}
pack();
}
}
}