source: trunk/src/org/expeditee/items/widgets/JfxMedia.java@ 963

Last change on this file since 963 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 3.5 KB
Line 
1/**
2 * JfxMedia.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.items.widgets;
20
21import java.io.File;
22
23import javafx.application.Platform;
24import javafx.beans.value.ChangeListener;
25import javafx.beans.value.ObservableValue;
26import javafx.embed.swing.JFXPanel;
27import javafx.scene.Group;
28import javafx.scene.Scene;
29import javafx.scene.media.Media;
30import javafx.scene.media.MediaPlayer;
31import javafx.scene.media.MediaView;
32
33import org.expeditee.items.Text;
34
35public class JfxMedia extends DataFrameWidget {
36
37 protected JFXPanel _panel;
38 protected MediaView _mediaView;
39 protected String _media;
40
41 public JfxMedia(Text source, String[] args) {
42 super(source, new JFXPanel(), -1, 500, -1, -1, 300, -1);
43
44 _panel = (JFXPanel) _swingComponent;
45 _media = (args != null && args.length > 0) ? args[0] : "";
46 System.out.println(_media);
47
48 Platform.runLater(new Runnable() {
49 @Override
50 public void run() {
51 initFx();
52 }
53 });
54 }
55
56 private void initFx() {
57 try {
58 Scene scene = new Scene(new Group());
59 _panel.setScene(scene);
60
61 Media media;
62 File f;
63 if((f = new File(this._media)).exists()) {
64 media = new Media(f.toURI().toString());
65 } else {
66 media = new Media(this._media);
67 }
68
69 // Create the player
70 final MediaPlayer mediaPlayer = new MediaPlayer(media);
71
72 System.out.println("Initial: " + mediaPlayer.getStatus());
73 mediaPlayer.statusProperty().addListener(new ChangeListener<MediaPlayer.Status>() {
74 @Override
75 public void changed(ObservableValue<? extends MediaPlayer.Status> observable, MediaPlayer.Status oldStatus, MediaPlayer.Status curStatus) {
76 System.out.println("Current: " + curStatus);
77 }
78 });
79
80 if (mediaPlayer.getError() != null) {
81 System.out.println("Initial Error: " + mediaPlayer.getError());
82 mediaPlayer.getError().printStackTrace();
83 }
84
85 mediaPlayer.setOnError(new Runnable() {
86 @Override public void run() {
87 System.out.println("Current Error: " + mediaPlayer.getError());
88 mediaPlayer.getError().printStackTrace();
89 }
90 });
91
92 // Create the view and add it to the Scene.
93 _mediaView = new MediaView(mediaPlayer);
94 ((Group) scene.getRoot()).getChildren().add(_mediaView);
95 mediaPlayer.play();
96 } catch (Exception e) {
97 e.printStackTrace();
98 }
99 }
100
101 @Override
102 protected String[] getArgs() {
103 return new String[] { this._media };
104 }
105}
Note: See TracBrowser for help on using the repository browser.