Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603719
  • 博文数量: 110
  • 博客积分: 8016
  • 博客等级: 中将
  • 技术积分: 1217
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-28 10:14
文章分类

全部博文(110)

文章存档

2008年(1)

2007年(13)

2006年(96)

我的朋友

分类: Java

2006-07-04 18:52:43

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) |
给主人留下些什么吧!~~