VLCJ:随着视频的进展更新进度条

时间:2012-07-12 17:58:05

标签: java multithreading swing vlcj

请查看以下代码

import com.sun.jna.Native;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

import com.sun.jna.NativeLibrary;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.bean.playerbean.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerEventListener;

public class PlayMedia extends JFrame
{
    private EmbeddedMediaPlayerComponent m;
    private JProgressBar bar;

    public PlayMedia()
    {

        NativeLibrary.addSearchPath(
      RuntimeUtil.getLibVlcLibraryName(), "c:\\program files\\videolan\\vlc"
    );
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);


        m = new EmbeddedMediaPlayerComponent();
        m.getMediaPlayer().addMediaPlayerEventListener(new UpdateBar());

        bar = new JProgressBar(0,100);
        bar.setStringPainted(true);

        getContentPane().add(m);
        getContentPane().add(bar,"South");


        this.setSize(500,500);
        this.validate();
        this.setLocationRelativeTo(null);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


         m.getMediaPlayer().playMedia("C:\\Users\\Yohan\\Desktop\\video.avi");   

    }

    private class UpdateBar extends MediaPlayerEventAdapter
    {
        public void positionChanged(MediaPlayer mp, float pos)
        {
            int value = Math.min(100, Math.round(pos * 100.0f));
            bar.setValue(value);
        }
    }

    public static void main(String[]args)
    {


            SwingUtilities.invokeLater(new Runnable() 
            {
            @Override
                public void run() 
                {
                    new PlayMedia();
                }
            });


    }
}

在这里,视频播放正常,但进度条没有更新。如何在视频播放时更新进度条?请帮忙!

2 个答案:

答案 0 :(得分:0)

我没有在Swing中使用ProgressBars,但我在Silverlight中使用过它们,所以我认为大多数原则都是一样的。在这里查看我的问题的答案: How do I make a Silverlight Progressbar update according to its value?它可能无法准确地向您展示如何在Swing中执行此操作,但似乎您在代码中使用了线程,这就是我的问题所在。

答案 1 :(得分:0)

由于某些未知原因,视频期间未调用positionChanged()。 尝试使用timeChanged()istead,它对我有用!

public void positionChanged (MediaPlayer mediaPlayer , long time) { ... }