📅  最后修改于: 2020-10-14 08:54:55             🧑  作者: Mango
让我们进一步增强应用程序,在其中我们将更新具有错误处理功能的vlcj-成品视频事件一章。
使用以下语法(模板方法)添加错误事件处理程序:
mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
@Override
public void error(MediaPlayer mediaPlayer) {
}
};
或者我们可以使用以下语法(侦听器方法)添加错误事件处理程序:
mediaPlayerComponent.getMediaPlayer().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
@Override
public void error(MediaPlayer mediaPlayer) {
}
});
如果视频加载失败,则会引发错误事件。但是随着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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
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;
private JButton pauseButton;
private JButton rewindButton;
private JButton skipButton;
public App(String title) {
super(title);
mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
@Override
public void playing(MediaPlayer mediaPlayer) {
super.playing(mediaPlayer);
System.out.println("Media Playback started.");
}
@Override
public void finished(MediaPlayer mediaPlayer) {
super.playing(mediaPlayer);
System.out.println("Media Playback finished.");
}
@Override
public void error(MediaPlayer mediaPlayer) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("Failed to load Media.");
}
});
}
};
}
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);
pauseButton = new JButton("Pause");
controlsPane.add(pauseButton);
rewindButton = new JButton("Rewind");
controlsPane.add(rewindButton);
skipButton = new JButton("Skip");
controlsPane.add(skipButton);
contentPane.add(controlsPane, BorderLayout.SOUTH);
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().play();
}
});
pauseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().pause();
}
});
rewindButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
}
});
skipButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
}
});
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应用程序运行。成功启动后,如果一切正常,则应显示以下结果-
现在将在控制台中看到如下消息:
Failed to load Media.