22750124
发贴: 17
积分: 0
|
于 2005-12-15 23:33
package mediaplayer;
import java.awt.*; import java.awt.event.*; import java.io.*;
import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.basic.*; import javax.media.*;
/** * * @author 权威 * @function AMP媒体播放器 * @version 1.0 * @date 2005-12-9 * */
public class AMP extends Frame implements ActionListener,ItemListener,ControllerListener{
private File oldFile,newFile; private BasicDirectoryModel bdm; private String[] s; private Player player; private Component visualcomponent,controlcomponent; private boolean firstState=true,loopState=false; private String currentDirectory;
private AMPMenuBar mb=new AMPMenuBar();
public AMP(String s) { super(s); //给菜单栏中加监听器 mb.open.addActionListener(this); mb.exit.addActionListener(this); mb.loop.addActionListener(this); mb.aboutAMP.addActionListener(this);
//将菜单栏加入窗体中 this.setMenuBar(mb); this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){ dispose(); //这里没有用System.exit(0) //因为用户点击关闭按钮调用dispose() //以执行windowClosed }
public void windowClosed(WindowEvent e){ //关闭未关闭的player if(player!=null){ player.close(); bdm.renameFile(newFile,oldFile); } System.exit(0); } });
this.setSize(500,300); this.setVisible(true); }
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("退出")){ //调用dispose()以便执行windowClosed dispose(); return; }
if(e.getActionCommand().equals("关于 AMP 1.0 播放器")){ JDialog jd=new JDialog(this,"关于 AMP 1.0 播放器",true); jd.getContentPane().add(new JLabel(" 欢迎您使用 AMP1.0 播放器")); jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); jd.setSize(200,100); jd.setVisible(true); }
if(e.getActionCommand().equals("打开...")){ FileDialog openFD=new FileDialog( this,"打开媒体文件",FileDialog.LOAD); openFD.setDirectory(currentDirectory); openFD.setVisible(true); //如果用户放弃选择文件,则返回
if(openFD.getFile()==null){ return; }
currentDirectory=openFD.getDirectory();
if(player!=null){ player.close(); bdm.renameFile(newFile,oldFile); }
JFileChooser openFile=new JFileChooser(currentDirectory); bdm=new BasicDirectoryModel(openFile); s=openFD.getFile().split("\\."); oldFile=new File(currentDirectory+openFD.getFile()); newFile=new File(currentDirectory+s[0]+".mpg");
//创建player try{ if(bdm.renameFile(oldFile,newFile)){ player=Manager.createPlayer( new MediaLocator( "file:"+openFD.getDirectory()+ s[0]+".mpg")); } }catch(IOException ioe){ System.err.println(ioe); bdm.renameFile(newFile,oldFile); return; }catch(NoPlayerException npe){ System.err.println("不支持的格式"); bdm.renameFile(newFile,oldFile); return; }
if(player==null){ System.out.println("无法创建播放器"); bdm.renameFile(newFile,oldFile); return; }
firstState=false; this.setTitle(openFD.getFile()); player.addControllerListener(this); //player加载 player.prefetch(); } }
public void itemStateChanged(ItemEvent e){ //设置loopState的value loopState=!loopState; }
public void controllerUpdate(ControllerEvent e){
//调用player.close()时ControllerClosedEvent事件出现 //如果存在视觉部件,则该部件应该拆除(为一致起见, //我们对控制面板部件也执行同样的操作)
if(e instanceof ControllerClosedEvent){
if(visualcomponent!=null){ remove(visualcomponent); visualcomponent=null; }
if(controlcomponent!=null){ remove(controlcomponent); controlcomponent=null; }
return; }
if(e instanceof EndOfMediaEvent){ //结束时的循环处理 if(loopState){ player.setMediaTime(new Time(0)); player.start(); }
return; }
if(e instanceof PrefetchCompleteEvent){ player.start(); return; }
if(e instanceof RealizeCompleteEvent){
visualcomponent=player.getVisualComponent();
if(visualcomponent!=null){ add(visualcomponent); }
controlcomponent=player.getControlPanelComponent();
if(controlcomponent!=null){ add(controlcomponent,BorderLayout.SOUTH); }
this.pack(); } }
public void paint(Graphics g){
if(firstState){
int width=this.getSize().width; int height=this.getSize().height;
g.setColor(new Color(142,176,219)); g.fillRect(0,0,width,height);
Font f=new Font("DialogInput",Font.BOLD,26); g.setFont(f);
FontMetrics fm=g.getFontMetrics(); int swidth=fm.stringWidth("☆☆☆ 欢迎 ☆☆☆");
g.setColor(Color.BLACK); g.drawString("☆☆☆ 欢迎 ☆☆☆", (width-swidth)/2, (height+this.getInsets().top)/2); }
//调用超类JFrame的paintComponent(), //该paintComponent()方法将调用JFrame包含的各个容器具 //和部件(包括控制面板部件)的paintComponent方法
super.paint(g); }
//不执行背景清除操作,以免控制面板部件闪烁 public void update(Graphics g){ paint(g); }
/** * @param args */
public static void main(String[] args) { new AMPJprogressBar("AMP 1.0 媒体播放器"); new AMP("AMP 1.0 媒体播放器"); }
} 为什么在连打开媒体文件后,会有部分媒体文件的名字没有被改回。仍为*.mpg
why edited on 2005-12-16 05:45
|