如何使用vlcj框架制作可以成为视频时间轴的Jslider

时间:2017-01-11 07:21:33

标签: java swing jslider vlcj

我遇到了一些麻烦,让我的jslider与我播放的视频同步。这是我的示例代码。

public Test() {


NativeLibrary.addSearchPath( RuntimeUtil.getLibVlcLibraryName(), "lib");
      Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);

        mediaPlayer = new EmbeddedMediaPlayerComponent(); 
          p2.setLayout(newBorderLayout());
p2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);  
b2.setIcon(new ImageIcon("pause.png"));
b2.revalidate();
b3.setIcon(new ImageIcon("skip.png"));
b3.revalidate();
b3.repaint();
p2.add(mediaPlayer);
mediaPlayer.getMediaPlayer().playMedia("intranet.mp4");
   slider.setValue((int) mediaPlayer.getMediaPlayer().getLength());  
      }

然后,在我的按钮上这是我的示例代码..

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    int time = (int) mediaPlayer.getMediaPlayer().getLength();


    JOptionPane.showMessageDialog(null,time);

}                                        

private void sliderStateChanged(javax.swing.event.ChangeEvent evt) {                                    

    label1.setText("Value" + slider.getValue());
}                                   

private void b3ActionPerformed(java.awt.event.ActionEvent evt) {                                   

    mediaPlayer.getMediaPlayer().skip(10000);
    JOptionPane.showMessageDialog(null, mediaPlayer.getMediaPlayer().getLength());
}                                  

private void b2ActionPerformed(java.awt.event.ActionEvent evt) {                                   

    mediaPlayer.getMediaPlayer().pause();
    b2.removeAll();
}                                  

private void b1ActionPerformed(java.awt.event.ActionEvent evt) {                                   


    mediaPlayer.getMediaPlayer().skip(-10000);
    int time = (int) mediaPlayer.getMediaPlayer().getLength();
    JOptionPane.showMessageDialog(null, mediaPlayer.getMediaPlayer().getLength());



}   

我的问题是我可以知道我播放的视频的总时间,并且在播放视频时我没有很多想法,然后滑块也会以指定的时间运行。提前感谢您的建议,我期待着它!

1 个答案:

答案 0 :(得分:0)

使用重写方法:Timechanged并将其链接到jslider,每次发送新的即时视频以更改滑块进度。

mediaplayer = new EmbeddedMediaPlayerComponent();

        embededMediaPlayer = mediaplayer.getMediaPlayer();
        embededMediaPlayer.setRepeat(true);
        videoSurface = mediaplayer.getVideoSurface();

        embededMediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            /**
             *
             * @param mediaPlayer play video
             * @param newTime get every millisecond changed in the mediaplayer
             * when is in playing mode
             */
            @Override
            public void timeChanged(MediaPlayer mediaPlayer, long newTime) {
                super.timeChanged(mediaPlayer, newTime);
                // here put you code to set new slider progress value 
                }
            });