JMF简单示例

java 代码
 
  1. import  java.awt.*;  
  2. import  java.awt.event.*;  
  3. import  javax.swing.*;  
  4. import  javax.media.*;  
  5.   
  6. public   class  VideoDemo  extends  JFrame  implements  ActionListener,ControllerListener  
  7. {  
  8. JMenuBar jMenuBar1=new  JMenuBar();  
  9. JMenu jMenuFile=new  JMenu( "File" );  
  10. JMenu jMenuList=new  JMenu( "List" );  
  11. JMenu jMenuControl=new  JMenu( "Control" );  
  12. JMenu jMenuSkin=new  JMenu( "Skin" );  
  13. JMenu jMenuHelp=new  JMenu( "Help" );  
  14. JMenuItem jMenuFileOpen=new  JMenuItem( "Open" );  
  15. JMenuItem jMenuFileExit=new  JMenuItem( "Exit" );  
  16. JMenuItem jMenuControlPlay=new  JMenuItem( "Play" );  
  17. JMenuItem jMenuControlPause=new  JMenuItem( "Pause" );  
  18. JMenuItem jMenuControlStop=new  JMenuItem( "Stop" );  
  19. JMenuItem jMenuSkinmetal=new  JMenuItem( "metalSkin" );  
  20. JMenuItem jMenuSkinwindows=new  JMenuItem( "windowsSkin" );  
  21. JMenuItem jMenuSkinmotif=new  JMenuItem( "motifSkin" );  
  22. JMenuItem jMenuControlFullScreen=new  JMenuItem( "FullScreen" );  
  23. JLabel statusBar=new  JLabel();  
  24. JLabel welcome=new  JLabel();  
  25. Player player;  
  26. Component visualComponent;//Video player component   
  27. Component controlComponent;//video player control component   
  28. JPanel jPanel1=new  JPanel( new  BorderLayout());  
  29. String file="" ;  
  30. String strDir;  
  31.   
  32. public  VideoDemo()  
  33. {     
  34. this .setSize( 400 , 300 );  
  35. this .setTitle( "Casper's player" );  
  36. welcome=new  JLabel( new  ImageIcon( "girl.jpg" ));  
  37. jPanel1.add(welcome);  
  38. statusBar.setText(" " );  
  39. jMenuFileExit.addActionListener(this );  
  40. jMenuFileOpen.addActionListener(this );  
  41. jMenuFile.add(jMenuFileOpen);  
  42. jMenuFile.addSeparator();  
  43. jMenuFile.add(jMenuFileExit);  
  44. jMenuBar1.add(jMenuFile);  
  45.   
  46. jMenuBar1.add(jMenuSkin);  
  47. jMenuSkin.add(jMenuSkinmetal);  
  48. jMenuSkinmetal.addActionListener(this );  
  49. jMenuSkin.add(jMenuSkinwindows);  
  50. jMenuSkinwindows.addActionListener(this );  
  51. jMenuSkin.add(jMenuSkinmotif);  
  52. jMenuSkinmotif.addActionListener(this );  
  53.   
  54. jMenuBar1.add(jMenuControl);/   
  55. jMenuControl.add(jMenuControlPlay);  
  56. jMenuControlPlay.addActionListener(this );  
  57. jMenuControl.add(jMenuControlPause);  
  58. jMenuControlPause.addActionListener(this );  
  59. jMenuControl.add(jMenuControlStop);  
  60. jMenuControlStop.addActionListener(this );  
  61. jMenuControl.add(jMenuControlFullScreen);///   
  62. jMenuControlFullScreen.addActionListener(this );  
  63.   
  64. jMenuBar1.add(jMenuList);  
  65. jMenuBar1.add(jMenuHelp);  
  66. this .setJMenuBar(jMenuBar1);  
  67. statusBar.setText("No file open" );  
  68. Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new  Boolean( true ));  
  69. //   
  70. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  71. this .getContentPane().add(statusBar, "South" );  
  72. this .getContentPane().add(jPanel1, "Center" );  
  73. this .setVisible( true );  
  74. }  
  75.   
  76. public   void  actionPerformed(ActionEvent e)  
  77. {  
  78.     //Skin menu(three skins)   
  79.     if (e.getSource()==jMenuSkinwindows)  
  80.     {  
  81.         String windows="com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ;  
  82.         try {  
  83.             UIManager.setLookAndFeel(windows);//设置见面风格   
  84.             SwingUtilities.updateComponentTreeUI(this ); //使设置的界面风格生效   
  85.             this .pack();  
  86.             }catch (Exception e1){  
  87.                 System.out.println("can't set such style" );  
  88.             }  
  89.     }  
  90.     else   if (e.getSource()==jMenuSkinmetal)  
  91.     {  
  92.         String metal="javax.swing.plaf.metal.MetalLookAndFeel" ;  
  93.         try {  
  94.             UIManager.setLookAndFeel(metal);  
  95.             SwingUtilities.updateComponentTreeUI(this );  
  96.             this .pack();  
  97.             }catch (Exception e1){  
  98.                 System.out.println("can't set such style" );  
  99.             }  
  100.     }  
  101.     else   if (e.getSource()==jMenuSkinmotif)  
  102.     {  
  103.         String motif="com.sun.java.swing.plaf.motif.MotifLookAndFeel" ;  
  104.         try {  
  105.             UIManager.setLookAndFeel(motif);  
  106.             SwingUtilities.updateComponentTreeUI(this );  
  107.             this .pack();  
  108.             }catch (Exception e1){  
  109.                 System.out.println("can't set such style" );  
  110.             }  
  111.     }  
  112. //   
  113.      
  114. //File menu   
  115. else   if (e.getSource()==jMenuFileExit)  
  116. {  
  117. if (player!= null )  
  118.     player.close();  
  119. this .dispose();  
  120. System.exit(0 );  
  121. }  
  122. else   if (e.getSource()==jMenuFileOpen)  
  123. {  
  124. try   
  125.    {  
  126.    jPanel1.remove(welcome);  
  127.    FileDialog dlgOpen=new  FileDialog( this , "Open file" ,FileDialog.LOAD);  
  128.    dlgOpen.setDirectory(strDir);  
  129.    dlgOpen.setVisible(true );  
  130.    strDir=dlgOpen.getDirectory();  
  131.    if (strDir!= null )  
  132.     {  
  133.     if (player!= null )  
  134.         player.close();  
  135.     if (visualComponent!= null )  
  136.         jPanel1.remove(visualComponent);  
  137.     if (controlComponent!= null )  
  138.         jPanel1.remove(controlComponent);  
  139.     visualComponent=null ;  
  140.     controlComponent=null ;  
  141.     file=strDir+dlgOpen.getFile();  
  142.    // player=Manager.createPlayer(new URL("file:\\"+file));//create the object of the media will play   
  143.     player = Manager.createPlayer (new  MediaLocator("file: ” +file));  
  144.  
  145.     statusBar.setText(" playing:  "+file.toString());//display the file imfomation  
  146.     player.addControllerListener(this);  
  147.     player.prefetch();//get the file data  
  148.     }  
  149.    }catch(Exception ex){ex.printStackTrace();}  
  150. }  
  151.  
  152.      
  153. //control funcitons Menu  
  154. else if(e.getSource()==jMenuControlFullScreen)//Full screen  
  155. {    
  156.     dispose();    
  157.     setUndecorated(true);    
  158.     getGraphicsConfiguration().getDevice().setFullScreenWindow(this);    
  159.     setVisible(true);  
  160. }    
  161.      
  162. else if(e.getActionCommand().equals(" Play ")) //play  
  163. {  
  164.      if(player != null)  
  165.      {  
  166.              player.start();  
  167.      }  
  168.      return;  
  169. }  
  170. else if(e.getActionCommand().equals(" Pause ")) //pause  
  171. {  
  172.      if(player != null)  
  173.      {  
  174.              player.stop();  
  175.      }  
  176.      return;  
  177. }  
  178. else if(e.getActionCommand().equals(" Stop")) //stop   
  179. {  
  180.      if (player !=  null )  
  181.      {  
  182.         player.stop();  
  183.          player.setMediaTime(new  Time( 0 ));  
  184. }  
  185. return ;  
  186. }  
  187. }  
  188.   
  189. public   void  controllerUpdate(ControllerEvent e)  
  190. {  
  191. if (e  instanceof  PrefetchCompleteEvent)  
  192. {  
  193. player.start();  
  194. return ;  
  195. }  
  196. if (e  instanceof  RealizeCompleteEvent)  
  197. {  
  198. visualComponent=player.getVisualComponent();  
  199. if (visualComponent!= null )  
  200. jPanel1.add(visualComponent,BorderLayout.CENTER);  
  201.   
  202. controlComponent=player.getControlPanelComponent();//time shaft control   
  203. if (controlComponent!= null )  
  204. jPanel1.add(controlComponent,BorderLayout.SOUTH);  
  205. player.getGainControl(); //volume control conponent   
  206.   
  207. this .pack();  
  208. this .setResizable( false );  
  209. this .setVisible( true );  
  210. return ;  
  211. }  
  212. }  
  213.   
  214. public   static   void  main(String[] args)  
  215. {new  VideoDemo();}  
  216. }  


/*Player类中的很多方法只有在Player对象处于Realized的状态下才会被调用。
为了保证Player对象已经到达了该状态,
你需要使用Manager的createRealizePlayer()方法
来获得Player对象。但是对于start()方法来说,
你可以在Player对象到达Prefetched状态之前调用它,
它可以自动将Player的状态转换到Started状态*/

//Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//取出屏幕大小

//player.getGainControl().getControlComponent(); //是操作音量(增加)的可视组件volume control conponent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值