2013-12-22 93 views
0

當應用程序運行時,聽到聲音並且其他按鈕控件在框架上可見,但包含視頻的面板不顯示任何黑色屏幕,也不會顯示框架。vlcj視頻未顯示,但視頻聲音被聽到

public class MediaPlayer extends JPanel { 

private JFrame ourframe = new JFrame(); 
//Declares our media player component 
private EmbeddedMediaPlayerComponent mediaplayer; 
//This string holds the media URL path 
private String mediapath = ""; 
//This string holds the vlc URL path 
private final String vlcpath = "C:\\Program Files (x86)\\VideoLAN\\VLC"; 
private JPanel panel, panel2; 
private JButton play_btn, stop_btn, foward_btn, rewind_btn, enlarge_btn; 
private JSlider timeline; 

public MediaPlayer(String mediapath) { 
    this.mediapath = mediapath; 
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcpath); 
    mediaplayer = new EmbeddedMediaPlayerComponent(); 

    panel = new JPanel(); 
    panel.setLayout(new BorderLayout()); 
    panel.add(mediaplayer, BorderLayout.CENTER); //panel containing the video 

    play_btn = new JButton("play"); 
    stop_btn = new JButton("stop"); 
    foward_btn = new JButton("ff"); 
    rewind_btn = new JButton("rew"); 
    enlarge_btn = new JButton("enlarge"); 

    timeline = new JSlider(0, 100, 0); 
    timeline.setMajorTickSpacing(10); 
    timeline.setMajorTickSpacing(5); 
    timeline.setPaintTicks(true); 

    panel2 = new JPanel(); 
    panel2.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    panel2.add(play_btn); 
    panel2.add(stop_btn); 
    panel2.add(foward_btn); 
    panel2.add(rewind_btn); 
    panel2.add(enlarge_btn); 
    panel2.add(timeline); 
    panel.add(panel2, BorderLayout.SOUTH); 
    add(panel); 
} 

public void play() { 
    mediaplayer.getMediaPlayer().playMedia(mediapath); 
} 

public static void main(String[] args) { 
    //Declare and initialize local variables 
    String mediaPath = ""; 
    mediaPath = "C:\\Users\\goldAnthony\\Desktop\\Videos\\Whistle.mp4"; 

    //creates instances of the VlcPlayer object, pass the mediaPath and invokes the method "run" 
    MediaPlayer mediaplayer = new MediaPlayer(mediaPath); 
    JFrame ourframe = new JFrame(); 
    ourframe.setContentPane(mediaplayer); 
    ourframe.setSize(720, 560); 
    ourframe.setVisible(true); 
    mediaplayer.play(); 
    ourframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

}

+0

當心,所述'EmbeddedMediaPlayerComponent'是(AFAIK)重量級部件,它可能不與輕質組分發揮好。我的第一條建議是直接在'JFrame'上直接播放媒體文件,看看它是如何工作的。 – MadProgrammer

+0

EmbeddedMediaPlayerComponent被設計爲直接添加到輕量級組件。 – caprica

回答

1

這是因爲你要添加的視頻表面一個JPanel沒有選擇合適的佈局管理器。

默認情況下,JPanel有一個FlowLayout,這將根據它們的首選大小來佈置組件。

視頻面板沒有設置首選大小。

解決的辦法是在主面板上設置一個像BorderLayout這樣的佈局管理器。

因此,在構造函數的結尾,而不是這個......

add(panel); 

...這樣做...

setLayout(new BorderLayout()); 
add(panel, BorderLayout.CENTER); 

跨發佈在[1]。

[1] https://groups.google.com/forum/#!topic/vlcj-users/m0zG-fx4qm8