视频完成后自动关闭VLC播放器?

时间:2014-06-02 01:58:40

标签: java video vlcj

因此,当我运行程序时,我正在使用VLCJ和用于java的VLC播放器播放视频。问题是,视频播放器仅在用户点击“x”按钮时关闭。有没有办法在视频结束时自动关闭它?

谢谢!

如果有帮助,这是我的代码:

//////Main class:

package switchAndAnim;

import java.io.File;

import javax.swing.JFileChooser;

public class Start {

    public static void main(String[] args)
    {
        //location of vlc files, media file
        new MediaPlayer("vlc-2.0.2", "ryankilp2.wmv").run();
    }

}

///////MediaPlayer class:
package switchAndAnim;
import javax.swing.JFrame;
import com.sun.jna.NativeLibrary;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

public class MediaPlayer {
private JFrame ourFrame = new JFrame();

    private EmbeddedMediaPlayerComponent ourMediaPlayer;

    private String mediaPath = "";

    MediaPlayer(String vlcPath,String mediaURL)
    {
        this.mediaPath = mediaURL;

        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlcPath);

        ourMediaPlayer = new EmbeddedMediaPlayerComponent();

        ourFrame.setContentPane(ourMediaPlayer);

        ourFrame.setSize(640,480);

        ourFrame.setVisible(true);

        ourFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void run()
    {
        ourMediaPlayer.getMediaPlayer().playMedia(mediaPath);
    }

}

1 个答案:

答案 0 :(得分:0)

媒体播放器可以收听您可以收听的各种事件,其中一个是在视频结束时触发的“已完成”事件。

ourMediaPlayer = new EmbeddedMediaPlayerComponent() {
    public void finished(MediaPlayer mediaPlayer) {
        ourMediaPlayer.release(); // In practice, this line is optional
        System.exit(0);
    }
}

我使用System.exit(),因为你的问题集EXIT_ON_CLOSE,但同样你可以根据你的使用情况隐藏或处理框架。