A SIMPLE EXAMPLE ABOUT HOW TO ENABLE VIDEO FILES IN JAVAFX APPLICATIONS Binh Nguyen Thanh ([email protected]) Saigon Institute of Technology This writing demonstrates using Java Media Framework API (JMF) in JavaFX applications through a simple example: play 2 video files, a mov file and an avi file in a JavaFX canvas, then drag and drop video files and control playback of video files. I. Download and Installation First, you install JDK 6 on your computer, in this writing uses JDK 6 update 4 (http://java.sun.com/javase/downloads/?intcmp=1281). Second, you install NetBeans IDE 6, in this writing uses NetBeans IDE 6.0 (http://download.netbeans.org/netbeans/6.0/final/). Third, you install JavaFX Script plug-in for NetBeans IDE 6.0, in this writing uses JavaFX Script plug-in for NetBeans IDE 6.0 version 1.0.5 (https://openjfx.dev.java.net/javafx-nb60-plugin-install.html). Last, you download Java Media Framework (JMF), in this writing uses JMF 2.1.1e cross platform Java (jmf-2_1_1e-alljava.zip) (http://java.sun.com/products/java-media/jmf/2.1.1/download.html). Addition, this writing also prepares 2 video files for testing purpose only; you can download them from attached source code belong to this writing. JavaOne: Mobile Video (http://sunfeedroom.sun.com): test.mov. Java Everywhere (http://sunfeedroom.sun.com): test.avi. II. Create a New JavaFX Script Application in NetBeans 6.0 IDE Start NetBeans 6.0 IDE New Project JavaFX JavaFX Script Application Next Enter Project Name Finish. Next, extract jmf-2_1_1e-alljava.zip to local folder, example JMF-2.1.1e folder, and then copy JMF-2.1.1e/lib/jmf.jar, JMF-2.1.1e/lib/jmf.properties and JMF2.1.1e/doc/readme.html to project root folder, example …/NetbeansProjects/SimpleMediaExample folder. After that, back to NetBeans 6.0 IDE choose Projects tab right click Libraries Add JAR/Folder… browse to project root folder choose jmf.jar open. III. Create a New JavaFX Class: SimpleMediaWidget New File… JavaFX JavaFX Class Next Enter File Name Choose Package Finish. Next, enter following code to implement SimpleMediaWidget class. package simplemediaexample; import javafx.ui.Widget; import java.net.URL; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.media.Manager; class SimpleMediaWidget extends Widget { attribute url: URL; attribute autoPlay: Boolean; } operation SimpleMediaWidget.createComponent() { var panel = new JPanel(new BorderLayout()); Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); var player = Manager.createRealizedPlayer(url); panel.add(player.getVisualComponent(), BorderLayout.CENTER); panel.add(player.getControlPanelComponent(), BorderLayout.SOUTH); if (autoPlay) { player.start(); } return panel; } IV. Play Video Files in JavaFX Application Back to Main class, Main.fx, simple application in example has a main window with title “Simple Media Example”. package simplemediaexample; import javafx.ui.Frame; import java.lang.System; Frame { title: 'Simple Media Example' onClose: operation() { System.exit(0); } visible: true } In example, application plays 2 video files concurrently. Video files should be contained in SimpleMediaWidget objects. And because SimpleMediaWidget objects must be placed on a Canvas object, SimpleMediaWidget objects should be wrapped in View objects. For testing more simpler, video files (test.mov, test.avi) will be copied to project root directory, example …/NetBeansProjects/SimpleMediaExample/, you can get these video files from attachment that belong to this writing. package simplemediaexample; import javafx.ui.Frame; import javafx.ui.Canvas; import javafx.ui.canvas.View; import java.lang.System; import java.io.File; Frame { title: 'Simple Media Example' content: Canvas { content: [ View { var xpos = 0 var ypos = 0 transform: bind translate(xpos, ypos) onMouseDragged: operation(e) { xpos += e.localDragTranslation.x; ypos += e.localDragTranslation.y; } content: SimpleMediaWidget { url: new File('test.mov').toURI().toURL() autoPlay: true } }, View { var xpos = 100 var ypos = 100 opacity: .5 transform: bind translate(xpos, ypos) onMouseDragged: operation(e) { xpos += e.localDragTranslation.x; ypos += e.localDragTranslation.y; } content: SimpleMediaWidget { url: new File('test.avi').toURI().toURL() autoPlay: true } } ] } onClose: operation() { System.exit(0); } visible: true } V. Run Example Projects tab right click on SimpleMediaExample project Run Project.
© Copyright 2026 Paperzz