-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJavaFXMediaPlayer.java
68 lines (56 loc) · 2.48 KB
/
JavaFXMediaPlayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.stage.Stage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.datavyu.plugins.MediaPlayerWindow;
import org.datavyu.plugins.ffmpeg.FfmpegSdlMediaPlayer;
import java.io.File;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class JavaFXMediaPlayer extends Application {
private static Logger logger = LogManager.getLogger(JavaFXMediaPlayer.class);
private JMediaPlayerControlFrame controller;
private MediaPlayerWindow mediaPlayer;
private URI mediaPath;
@Override
public void start(Stage primaryStage) {
mediaPath = new File("Nature_30fps_1080p.mp4").toURI();
MediaPlayerTask mediaPlayerTask = new MediaPlayerTask(mediaPath);
mediaPlayerTask.setOnSucceeded(
event -> {
mediaPlayer = mediaPlayerTask.getValue();
mediaPlayer.addMediaErrorListener(
// Handle error thrown by The Media Player
(source, errorCode, message) -> logger.error(errorCode + ": " + message));
// Handle Window Key events triggered from SDL window
mediaPlayer.addSdlKeyEventListener(
(source, nativeMediaRef, javaKeyCode) ->
controller.handleKeyEvents(javaKeyCode));
// Open a simple JFrame to control the media player through key commands
// Be creative and create your own controller in JavaFX
Platform.runLater(
() -> {
controller = new JMediaPlayerControlFrame(mediaPlayer);
});
});
mediaPlayerTask.setOnFailed(event -> Platform.exit());
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(mediaPlayerTask);
executorService.shutdown();
}
}
class MediaPlayerTask extends Task<MediaPlayerWindow> {
private URI mediaPath;
MediaPlayerTask(URI mediaPath) {
this.mediaPath = mediaPath;
}
@Override
protected MediaPlayerWindow call() throws Exception {
MediaPlayerWindow mediaPlayer = new FfmpegSdlMediaPlayer(mediaPath);
mediaPlayer.init();
return mediaPlayer;
}
}