2011-10-21 70 views
0

我設法使用JMF在Java中創建了一個臨時視頻播放器。源代碼如下。我想附加視頻效果,比如將每個幀轉換爲灰度,並使用JMF爲每個幀添加文本標題。JMF中的視頻效果

有關JMF視頻效果的信息似乎非常稀少。我將如何去創建過濾器(或編解碼器,或者他們所謂的任何東西)來完成上述任務?

import java.awt.*; 
import javax.swing.*; 
import javax.media.*; 
import javax.media.format.*; 
import javax.media.protocol.*; 
import javax.media.control.*; 
import java.net.URL; 
import java.net.MalformedURLException; 
import java.io.*; 


public class MediaPlayer extends JFrame 
{ 
    public MediaPlayer() 
    { 

    } 

    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame(); 
     frame.setLayout(new BorderLayout()); 

     try { 
      URL mediaURL = new File("video.avi").toURI().toURL(); 
      Player mediaPlayer = Manager.createRealizedPlayer(mediaURL); 
      Component video = mediaPlayer.getVisualComponent(); 
      Component controls = mediaPlayer.getControlPanelComponent(); 
      frame.add(video,BorderLayout.CENTER); 
      frame.add(controls,BorderLayout.SOUTH); 
      frame.setVisible(true); 
     } 

     catch (MalformedURLException e) { 
      System.out.println(e.toString()); 

     } 

     catch (IOException e) { 
      System.out.println(e.toString()); 
     } 

     catch (NoPlayerException e) { 
      System.out.println(e.toString()); 
     } 

     catch (CannotRealizeException e) { 
      System.out.println(e.toString()); 
     } 
    } 
} 

回答

1

嗨,這是我在任何論壇的第一篇文章,如此抱歉的錯誤。

附加你需要使用「處理器」

這裏的任何視頻效果是一個代碼示例處理器添加以及添加效果吧:

String strDevName = "your Media MRL"; 
     CaptureDeviceInfo devInfo = CaptureDeviceManager.getDevice(strDevName); 
     MediaLocator ml = devInfo.getLocator(); 
     DataSource ds; 
     Processor p; 
     try{ 
      ds = Manager.createDataSource(ml); 
      p = Manager.createProcessor(ds); 
      p.configure(); 
      while(p.getState() != p.Configured); 
      p.setContentDescriptor(null); 
      TrackControl[] controls = p.getTrackControls(); 
      controls[0].setFormat(new VideoFormat(VideoFormat.YUV));//Specify the Video format of the video specified in the MRL 
       Codec codec[]= { new comp311.jmf.effect.GreyEffect() };//class GrayEffect is a implementation of javax.media.Effect (the link for the class given below) 
      controls[0].setCodecChain(codec); 
      p.realize(); 
      while(p.getState() != p.Realized); 
      p.prefetch(); 
      while(p.getState() != p.Prefetched); 
      video = p.getVisualComponent(); 
      if (video != null) {System.out.println("Prefetched2"); 
       pnlVideo.add(video, BorderLayout.CENTER);//pnlVideo is a JPanel 
       p.start(); 

      } 
     }catch(Exception e){} 

the link for the effect class :


re:

while(p.getState() != p.Configured); 
while(p.getState() != p.Realized); 
while(p.getState() != p.Prefetched); 

在我的程序的這個地方,我停止了被迫執行,直到處理器達到一個狀態,但是如果狀態不可行,那麼這個prigram gos會進入一個無限循環。 JMF給出了一個StaeHelper類來克服它的谷歌問題。