2014-10-28 162 views
1

我嘗試在畫布內放置VLCJ播放器。 我已經閱讀了很多關於它的主題,但我仍然無法實現我想要做的事情。在畫布中顯示VLCJ播放器

這裏是我使用的代碼:

public class RaspberryControler extends JFrame 
{ 

    /*Have to declare it in order to use vlcj*/ 
    private EmbeddedMediaPlayerComponent mediaPlayerComponent; 
    private EmbeddedMediaPlayer mediaPlayer; 

    public RaspberryControler(String host){ 
     this.host = host; 
     controler = new Controler(this); 
     initComponents(); 
    } 

    private void initComponents(){  
     setBasicParameters(); 
     createMainPanel(); 
     createControlPanel(); 
     createWebcamPanel(); 
     mainPanel.add(webcamPanel); 
     mainPanel.add(controlPanel); 
     setListeners();  

     /*Set the last parameters of the frame*/ 
     this.revalidate(); 
     this.repaint(); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null);  
    } 

    private void createMainPanel(){ 

     mainPanel = new JPanel(); 

     /*Set the Layout*/ 
     mainPanel.setLayout(new FlowLayout()); 
     mainPanel.setVisible(true); 

     /*Set the parameters*/ 
     this.getContentPane().add(mainPanel); 
    } 

    private void createWebcamPanel(){  
     /*Get the VLC Libraries*/ 
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:/VLC/VideoLAN/VLC"); 
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); 

    /*Create components*/ 
    liveStream = new JLabel("Live Stream"); 
    mediaPlayerComponent = new EmbeddedMediaPlayerComponent(); 

    /*Set parameters of the components*/ 
    liveStream.setPreferredSize(new Dimension(200, 30)); 
    liveStream.setHorizontalAlignment(SwingConstants.CENTER); 

    /*Set the layout*/ 
    webcamPanel = new JPanel(); 
    webcamPanel.setLayout(new BorderLayout()); 
    webcamPanel.setPreferredSize(new Dimension(550, 480)); 
    webcamPanel.setBorder(BorderFactory.createLineBorder(Color.RED)); 

    /*Place the components*/ 
    webcamPanel.setVisible(true); 
    webcamPanel.add(liveStream, BorderLayout.NORTH);   
    webcamPanel.add(mediaPlayerComponent, BorderLayout.CENTER); 

    this.setVisible(true); 

    try{ 
     mediaPlayerComponent.getMediaPlayer().playMedia("http://127.0.0.1:8989/movie"); 
    }catch(IllegalStateException e){ 
     System.out.println(e.toString()); 
    } 
} 

然後,當然,WebcamPanel被添加到當前的JFrame。

我做錯了什麼?

這是輸出:The video surface component must be displayable 感謝那些會迴應!

PS:這裏是我籤的主題: - https://github.com/caprica/vlcj/issues/29 - Failed to play video by vlcj in java

回答

1

其實你不說的正是不工作。

就它而言,它看起來像缺少佈局約束。

這大概:

webcamPanel.add(canvas); 

應該是這樣的:

webcamPanel.add(canvas, BorderLayout.CENTER); 

否則你是一個null佈局約束添加您的畫布。

「視頻組件必須是可顯示的」錯誤是因爲直到用戶界面可顯示才能播放視頻-VLC沒有用於Canvas的有效窗口句柄,除非您或者是pack()幀,或者使其可見。總之,在播放視頻之前,你應該做frame.setVisible(true)

與線程相關,通常使用Swing線程規則 - 即您必須確保在事件調度線程(EDT)上調用對UI組件的更改。通常情況下,您不需要擔心這一點,比如當您響應Swing事件偵聽器時。如果您有後臺線程(而不是Swing EDT),則只需要擔心此問題,該後臺線程會啓動Swing組件的創建或操作。如果您需要這樣做,請在SwingUtilities類上使用invokeLater。我認爲這不是你需要的,但是沒有更多的代碼很難說。

一個稍微偏離主題但相關的評論:與vlcj的現代版本,有更簡單的方法來做到這一點,例如,通過使用EmbeddedMediaPlayerComponent它可以幫助您處理媒體播放器,視頻表面和相關的Canvas。您只需將組件添加到您的佈局,即可完成。

+0

謝謝你的回答!我更新了我的問題以更加準確,現在我使用EmbeddedMediaPlayer。不過,我有這個輸出:視頻表面組件必須是可顯示的。你知道這是什麼意思嗎? – Mornor 2014-10-28 15:34:09

+0

謝謝你給我的幫助!我編輯了這個問題,以便代碼是我現在擁有的。我在嘗試播放流之前將幀設置爲可見,但我仍然有相同的錯誤。我不知道爲什麼...... – Mornor 2014-10-28 21:46:27

+0

哦,是的,我的面板中確實有一個像我的面板的玩家(像黑色方塊),但錯誤當然還是存在。 – Mornor 2014-10-28 21:48:34

相關問題