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

📄 videomidlet.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import java.io.InputStream; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 
import javax.microedition.lcdui.*; 
import javax.microedition.media.*; 
import javax.microedition.media.control.*; 
import javax.microedition.midlet.*; 

public class VideoMIDlet extends MIDlet implements CommandListener, 
      PlayerListener, Runnable { 
   private Display display; 
   private Form form; 
   private TextField url; 
   private Command start = new Command("Play", Command.SCREEN, 1); 
   private Command stop = new Command("Stop", Command.SCREEN, 2); 
   private Player player; 

   public VideoMIDlet() { 
      display = Display.getDisplay(this); 
      form = new Form("Demo Player"); 
	  //创建输入视频URL地址的文本框
      url = new TextField("Enter URL:", "", 100, TextField.URL); 
      form.append(url); 
      form.addCommand(start); 
      form.addCommand(stop); 
      form.setCommandListener(this); 
      display.setCurrent(form); 
   } 

   protected void startApp() { 
      try { 
		  //判断如果播放器处于就绪状态,就播放视频
         if (player != null && player.getState() == Player.PREFETCHED) { 
            player.start(); 
         } else { 
            defplayer(); 
            display.setCurrent(form); 
         } 
      } catch (MediaException me) { 
         reset(); 
      } 
   } 

   protected void pauseApp() { 
      try { 
		  //如果播放器处于播放状态,则停止播放
         if (player != null && player.getState() == Player.STARTED) { 
            player.stop(); 
         } else { 
            defplayer(); 
         } 
      } catch (MediaException me) { 
         reset(); 
      } 
   } 

   protected void destroyApp(boolean unconditional) { 
      form = null; 
      try { 
         defplayer(); 
      } catch (MediaException me) { 
      } 
   } 

   public void playerUpdate(Player player, String event, Object data) { 
      if (event == PlayerListener.END_OF_MEDIA) { 
         try { 
            defplayer(); 
         } catch (MediaException me) { 
         } 
         reset(); 
      } 
   } 

   public void commandAction(Command c, Displayable d) { 
      if (c == start) { 
         start(); 
      } else if (c == stop) { 
         stopPlayer(); 
      } 
   } 

   public void start() { 
	   //创建播放线程
      Thread t = new Thread(this); 
      t.start(); 
   } 

   // 为了防止阻塞,网络连接应该被定义在一个线程中,而不是在commandAction()方法中 
   public void run() { 
      play(getURL()); 
   } 

   String getURL() { 
      return url.getString(); 
   } 

   void play(String url) { 
      try { 
         VideoControl vc; 
         defplayer(); 
         InputStream dis = null; 
		 //建立并打开HTTP连接
         HttpConnection con = (HttpConnection) Connector.open(url,Connector.READ); 
		 //打开网络输入流
         dis = con.openInputStream(); 
         if (dis != null) { 
			//创建播放器实例
            player = javax.microedition.media.Manager.createPlayer(dis,"video/mpeg"); 
            //添加消息监听器
            player.addPlayerListener(this); 
            // 准备播放信息
            player.realize(); 
			//创建视频控制接口
            vc = (VideoControl) player.getControl("VideoControl"); 
            if (vc != null) { 
			   //得到GUI界面组件
               Item video = (Item) vc.initDisplayMode(vc.USE_GUI_PRIMITIVE,null); 
               Form v = new Form("Playing Video..."); 
               StringItem si = new StringItem("Status: ", "Playing..."); 
               v.append(si); 
			   //将媒体播放器组件添加到屏幕上
               v.append(video); 
               display.setCurrent(v); 
            } 
         } 
         player.prefetch(); 
		 //播放视频
         player.start(); 
      } catch (Throwable t) { 
		  System.out.println(t);
         reset(); 
      } 
   } 

   void defplayer() throws MediaException { 
      if (player != null) { 
         if (player.getState() == Player.STARTED) { 
            player.stop(); 
         } 
         if (player.getState() == Player.PREFETCHED) { 
            player.deallocate(); 
         } 
         if (player.getState() == Player.REALIZED 
               || player.getState() == Player.UNREALIZED) { 
            player.close(); 
         } 
      } 
      player = null; 
   } 

   void reset() { 
      player = null; 
   } 

   void stopPlayer() { 
      try { 
         defplayer(); 
      } catch (MediaException me) { 
      } 
      reset(); 
   } 
} 

⌨️ 快捷键说明

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