2015-11-08 172 views
0

我想播放一個視頻使用vlcj內JPanel,但它不適用於我。消息例外,我得到的是:我試圖播放視頻使用vlcj內JPanel

「java.lang.IllegalStateException:視頻表面成分必須是可顯示」

代碼:

public class MediaPlayerPanel extends JPanel { 

    private static final long serialVersionUID = 1L; 


    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(); 
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer(); 
    Canvas c = new Canvas(); 
    JPanel p = new JPanel(); 



    public MediaPlayerPanel() { 



     c.setBackground(Color.black); 

     p.setLayout(new BorderLayout()); 
     p.add(c, BorderLayout.CENTER); 


     mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c)); 
     c.setVisible(true);  
     p.setVisible(true); 
    } 

    public void play(String video) { 

     mediaPlayer.playMedia(video); 

    } 

} 


public class VideoPlayer { 

    public static void main(final String[] args) { 

     NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC"); 
     Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); 

      JFrame frame = new JFrame("Video Player"); 
      MediaPlayerPanel mpp = new MediaPlayerPanel(); 

      frame.setLocation(100, 100); 
      frame.setSize(1050, 600); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


      frame.add(mpp, BorderLayout.CENTER); 
      frame.setVisible(true); 

      mpp.play("E:\\Filmek\\Game.of.Thrones.S04E02.HDTV.x264-2HD\\game.of.thrones.s04e02.hdtv.x264-2hd.mp4"); 


    } 
} 
+0

storno。我解決了。新JPanel無用... – reja79

+0

你是如何解決它的?這可能有助於未來的人。 –

+0

我刪除了不需要的JPanel。 c Canvas添加了MediaPlayerPanel。 – reja79

回答

0

該錯誤消息通常手段您在顯示(或pack()'ed)之前嘗試執行mediaPlayer.play()包含視頻表面的應用程序框架。

在您發佈的代碼中,您並未將面板p添加到您創建的MediaPlayerPanel面板。因此,您的視頻表面Canvas實際上並未附加到任何可見組件層次結構。

你的MediaPlayerPanel應該自己設置一個BorderLayout並且加上p即可。

+0

是的。那就對了。 – reja79