(转)java JMF的配置方法

倖福魔咒の 提交于 2019-12-01 12:21:23

java JMF的配置方法


 


Java推出的JMF(Java Media Framework)之后标着Java进入了多媒体时代,如果你想编写能播放视频的Java程序,必须下载sun公司的JMF2.1(或者更高的版本),它为我们提供了编写多媒体必须的包:java.media

 

没有安装JFM的在编译*.java的时候会提示找不到java.media包

 

java JMF的配置方法

1.首先去sun公司的主页去下载安装包: 点击链接下载

2.下载得到一个名为jdk-6u12-windows-i586-p 的文件

3.双击安装,默认的安装路径是C:\Program Files\JMF2.1.1e(可以根据自己的需要改变这个路径)

4.安装后,需要配置下面的几个变量(vista系统和XP相同),下面以vista系统为例子

  a.点击我的电脑--->属性--->高级系统设置--->环境变量

  b.找到CLASSPATH,分别增加jmf.jar和sound.jar

    C:\Program Files\JMF2.1.1e\lib\jmf.jar;

    C:\Program Files\JMF2.1.1e\lib\sound.jar;

   (其中 C:\Program Files\JMF2.1.1e为你的JFM的安装路径

  c.找到PATH,增加动态库

    C:\Program Files\JMF2.1.1e\bin;

    提示:每添加一项后,不要忘记了后面的分号

 

好运~

 

附一个用JAVA编写播放视频的程序

 

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.media.*;
public class JavaVideo extends Applet implements ControllerListener,Runnable,ItemListener
{
   Player player;
   String str;
   Thread mythread;
   Choice choice;
   Component visualCompoment,controlCompoment,progressBar;
   String mediaFile;
   URL mediaURL,codeBase;
   Frame frame;
   public void init()
   {
       str="music.MPE";
       mythread=new Thread(this);
       choice=new Choice();
       choice.add("music01.MPG");
       choice.add("music02.avi");
       choice.add("music03.avi");
       choice.addItemListener(this);
       frame=new Frame("Movie System");
       frame.setSize(640,480);
       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((visualCompoment=player.getVisualComponent())!=null)
                {
                  frame.add("Center",visualCompoment);
                }
             if((controlCompoment=player.getControlPanelComponent())!=null)
                  if(visualCompoment!=null)
                      frame.add("South",controlCompoment);
                  else
                      frame.add("Center",controlCompoment);
             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("Can't find a player"+mediaURL);
          }
        if(player!=null)
          {
              player.prefetch();
          }
   }  
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!