2016-09-09 144 views
0

我使用VLCJ在運行我的程序時播放視頻。視頻結束時有什麼方法可以自動關閉它嗎?我不會讓Keylistener關閉視頻用VLCJ播放器結束視頻?

謝謝!

代碼:

String file = "Estopa.mp4"; 

    public Test(){ 
     f.setLocation(100,100); 
     f.setSize(alto,ancho); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setVisible(true); 

     c.setBackground(Color.black); 
     p.setLayout(new BorderLayout());  
     p.add(c); 
     f.add(p); 

     // Read video file 
     // load native library 
     NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:/Program Files/VideoLAN/VLC"); 
     Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); 
     // initialize the media player 
     MediaPlayerFactory mpf = new MediaPlayerFactory(); 
     // control all the interactions with the user 
     EmbeddedMediaPlayer emp = mpf.newEmbeddedMediaPlayer(new Win32FullScreenStrategy(f)); 
     emp.setVideoSurface(mpf.newVideoSurface(c)); 
     // full screen 
     emp.toggleFullScreen();    
     //hide the cursor 
     emp.setEnableMouseInputHandling(true); 
     //able keyboard 
     emp.setEnableKeyInputHandling(true);   
     //prepare file to read 
     emp.prepareMedia(file); 
     // read the file 
     emp.play(); 



    } 

回答

1

添加MediaPlayerEventListener到媒體播放器。

在你的監聽器實現中,在finished方法中添加你想要的任何代碼。

事情是這樣的:

emp.addMediaPlayerEventListener(new MediaPlayerEventAdapter() { 
    @Override 
    public void finished(MediaPlayer mediaPlayer) { 
     System.exit(0); 
    } 
}); 

你也可能要添加一個實現了error回調,因爲如果有一個錯誤,你不會得到一個finished事件。

請注意MediaPlayerEventAdapter是一個MediaPlayerEventListener的空實現。