import java.applet.*;import java.awt.*;
import java.net.*;import java.awt.event.*;
import java.io.*;
import javax.media.*;
public class Video extends Applet
implements ControllerListener,Runnable,ItemListener
{ Player player;
String str;
Thread mythread;
Choice choice;
Component visualComponent,controlComponent,progressBar;
String mediaFile;
URL mediaURL,codeBase;
Frame frame;
public void init()
{ str="Music01.MPG";
mythread=new Thread(this);
choice=new Choice();
choice.add("Music01.MPG");
choice.add("Music02.avi");
choice.add("Music03.avi");
choice.addItemListener(this);
codeBase=getDocumentBase();
frame=new Frame("视频系统");
frame.setSize(660,580);
frame.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ if(player!=null)
{ player.stop();player.deallocate();
}
frame.setVisible(false);
System.exit(0);
}
});
add(choice);
}
public void stop()
{ if(player!=null)
{ player.stop();
}
}
public synchronized void controllerUpdate(ControllerEvent event)
{ player.getDuration();
if(event instanceof RealizeCompleteEvent)
{ if((visualComponent=player.getVisualComponent())!=null)
frame.add("Center",visualComponent);
if((controlComponent=player.getControlPanelComponent())!=null)
if(visualComponent!=null)
frame.add("South",controlComponent);
else
frame.add( "Center",controlComponent);
frame.validate();
frame.pack();
}
else if(event instanceof PrefetchCompleteEvent)
{ player.start();
}
}
public void itemStateChanged(ItemEvent e)
{
str=choice.getSelectedItem();
if(player==null)
{
}
else
{ player.stop();player.deallocate();
}
frame.removeAll();
frame.setVisible(true);
frame.setBounds(300,100,150,100);
frame.validate();
if(!(mythread.isAlive()))
{ mythread=new Thread(this);
}
try{
mythread.start();
}
catch(Exception ee)
{
}
}
public synchronized void run()
{ try{ mediaURL=new URL(codeBase,str);
player=Manager.createPlayer(mediaURL);player.getDuration();
if(player!=null)
{ player.addControllerListener(this);
}
else
System.out.println("failed to creat player for"+mediaURL);
}
catch(MalformedURLException e)
{ System.out.println("URL for"+mediaFile+"is invalid");
}
catch(IOException e)
{ System.out.println("URL for"+mediaFile+"is invalid");
}
catch(NoPlayerException e)
{ System.out.println("canot find a player for"+mediaURL);
}
if(player!=null)
{ player.prefetch();
}
}
}
阅读(2085) | 评论(0) | 转发(0) |