vlcj - 播放视频
VLC 播放器发现
vlcj 库提供了一个类,它使用以下语法自动发现系统中已安装的 VLC 播放器。
EmbeddedMediaPlayerComponent mediaPlayerComponent = = new EmbeddedMediaPlayerComponent();
加载视频
现在使用媒体,我们可以使用以下语法轻松地在我们的应用程序中加载视频−
mediaPlayerComponent.mediaPlayer().media().startPaused(path);
播放视频
现在使用控件,我们可以使用以下语法轻松地在我们的应用程序中播放视频−
mediaPlayerComponent.mediaPlayer().controls().play();
示例
在 Eclipse 中打开在环境设置一章中创建的项目 mediaPlayer。
使用以下代码更新 App.java −
App.java
package com.tutorialspoint.media; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent; public class App extends JFrame { private static final long serialVersionUID = 1L; private static final String TITLE = "My First Media Player"; private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4"; private final EmbeddedMediaPlayerComponent mediaPlayerComponent; private JButton playButton; public App(String title) { super(title); mediaPlayerComponent = new EmbeddedMediaPlayerComponent(); } public void initialize() { this.setBounds(100, 100, 600, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { mediaPlayerComponent.release(); System.exit(0); } }); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(mediaPlayerComponent, BorderLayout.CENTER); JPanel controlsPane = new JPanel(); playButton = new JButton("Play"); controlsPane.add(playButton); contentPane.add(controlsPane, BorderLayout.SOUTH); playButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { mediaPlayerComponent.mediaPlayer().controls().play(); } }); this.setContentPane(contentPane); this.setVisible(true); } public void loadVideo(String path) { mediaPlayerComponent.mediaPlayer().media().startPaused(path); } public static void main( String[] args ){ try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.out.println(e); } App application = new App(TITLE); application.initialize(); application.setVisible(true); application.loadVideo(VIDEO_PATH); } }
通过右键单击文件并选择以 Java 应用程序运行来运行应用程序。成功启动后,如果一切正常,则应显示以下结果 −
单击"播放"按钮,视频将开始播放。