⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example8_15.java.bak

📁 书中的例题
💻 BAK
字号:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.media.*;
public class Example8_15 extends Frame implements ActionListener,
             ControllerListener,Runnable,ItemListener
{
	Player player;
   String str; 
   Thread mythread;
   Choice choice;
   Component visualComponent,controlComponent,progressBar;
   String mediaFile;
   URL mediaURL,codeBase;
   Frame frame;
   Button btn;
   Example8_15()
   {
	 super("视频系统");
	 setSize(660,580);
	 setVisible(true);
	 btn=new Button("Play");
	 add(btn);
	 btn.addActionListener(this);
	 str="music01.mpg"; 
     mythread=new Thread(this);
     choice=new Choice();
     choice.add("music01.mpg");
     choice.add("music02.mpg");
     choice.add("Music03.avi");
     choice.addItemListener(this);
     //codeBase=getDocumentBase();


 
	 validate();
   } 

 public void actionPerformed(ActionEvent e)
   { 
          frame=new Frame("视频播放");
     frame.setSize(660,580); 
	 addWindowListener(new WindowAdapter()
            {  public void windowClosing(WindowEvent e)
               {  if(player!=null)
                    { 
		              player.stop();
	                  player.deallocate(); 
                    }
                 frame.setVisible(false);
                 System.exit(0);
               } 
            });
     frame.add(choice);
	 frame.setVisible(true);
     frame.validate();
   }

	public static void main(String[] args) 
	{
		new Example8_15();
	}

   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()
   {  
	   str="music02.mpg"; 
	   try{  mediaURL=new URL(str);
            player=Manager.createPlayer(new MediaLocator(str));
			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(); 
       }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -