vlcj - Gestione degli eventi del mouse
Miglioriamo ulteriormente l'applicazione in cui aggiorneremo vlcj - Errore durante il caricamento del capitolo Evento video con capacità di gestione degli eventi del mouse.
Gestione degli eventi del mouse
Aggiungi gestori di eventi del mouse utilizzando la seguente sintassi (metodo modello):
mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
}
};
Oppure possiamo aggiungere il gestore di eventi del mouse usando la seguente sintassi (metodo Listener):
Component videoSurface = mediaPlayerComponent.videoSurfaceComponent();
videoSurface.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
}
});
videoSurface.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
}
});
In caso di metodo Listener su Windows, è necessario disabilitare la gestione dell'input del mouse nativo utilizzando la seguente sintassi.
mediaPlayerComponent.mediaPlayer().input().enableMouseInputHandling(false);
Quando usiamo il mouse all'interno di una superficie video, verrà generato l'evento mouse.
Esempio
Apri il progetto mediaPlayer come creato nel capitolo Configurazione dell'ambiente in Eclipse.
Aggiorna App.java con il seguente codice−
App.java
package com.tutorialspoint.media;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
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.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 mouseClicked(MouseEvent e) {
super.mouseClicked(e);
System.out.println("Mouse Clicked. (" + e.getX() + "," + e.getY() + ")");
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
super.mouseWheelMoved(e);
System.out.println("Mouse wheel moved. " + e.getScrollAmount());
}
@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);
}
}
Esegui l'applicazione facendo clic con il pulsante destro del mouse sul file e scegli Esegui come applicazione Java. Dopo un avvio riuscito, se tutto va bene, dovrebbe visualizzare il seguente risultato:
Ora fai clic all'interno del video e vedrai un messaggio nella console come segue-
Media Playback started.
Media Playback started.
Mouse Clicked. (377,180)
Mouse Clicked. (377,180)
Mouse Clicked. (356,120)
Media Playback finished.