Monday Jun 29, 2009

Here's a quick-and-easy video player written in the JavaFX Script programming language. First, place your video clip into the same directory as this source code. Next, edit the code in red (clip name, scene with, and scene height) to match your particular video. Finally, compile and run the program. Clicking on the screen will pause/unpause playback.

Step 1: Add Import Statements

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.media.*;

Step 2: Define Media-Related Variables

def vid = "{__DIR__}video-clip.flv"; // CHANGE AS NEEDED

var isPlaying = false;

def myMediaPlayer = MediaPlayer {
        media: Media {source: vid}
}

def myMediaView = MediaView {

    onMousePressed: function(event){

        // take action
        if(isPlaying){
            myMediaPlayer.pause();
        }else{
            myMediaPlayer.play();
        }

        // toggle play state
            isPlaying = not isPlaying;
    }

    mediaPlayer: myMediaPlayer
}

Step 3: Render Player

function run(){
    Stage {
        title: "JavaFX Video Player"
        scene: Scene {
            width: 800 // CHANGE AS NEEDED
            height: 600 // CHANGE AS NEEDED

            fill: Color.BLACK
            content: [myMediaView]
        }
    }   
}
Comments:

Hi,

Any link on how to embed a video using JavaFX in a Swing app please?

Posted by 81.129.156.145 on June 29, 2009 at 03:47 PM PDT #

Hi,
How is this quicker or easier than HTML5s:
<video src="videofile.ogv" autoplay="autoplay" />

Shamefully Java missed the boat on this functionality, something it should have had a decade ago. Fancy embedding the video in a flv though, a competing plugin format anyway…

Posted by John Drinkwater on June 29, 2009 at 03:54 PM PDT #

@ first poster, JavaFX Video only works in JavaFX apps. However, you can embed Swing controls and panels in your JavaFX application.

@John, JavaFX works on existing desktops and browsers that already have Java 5 or higher installed. The user doesn't need to upgrade their browser, which is required for HTML 5.

Posted by Josh Marinacci on June 29, 2009 at 10:46 PM PDT #

Sorry for the random question, but I don't know where else to post it: when can we expect the JavaFX plugin for Netbeans 6.7? The plugin for 6.5.1 has "Format Source" broken which makes writing highly structured JavaFX code very painful.

Posted by 67.188.149.156 on June 29, 2009 at 11:41 PM PDT #

Josh,

I know you can embed swing elements in JavaFx, but this is another reason why I will not use JavaFX. I know you guys are pushing hard for JavaFX, but I have invested 10 years in Swing... not to let it go like that - just because it is the new thing.

Either you can embed JavaFX features in Swing, or I will not use it at all. JavaFX is too immature for enterprise apps to be developed with it.

Please Sun, look around.

Kind regards,

Posted by 86.149.162.73 on June 30, 2009 at 11:12 AM PDT #

Embedding JavaFX features inside of Swing and calling JavaFX objects from Java code is something we are considering for a future release. There are both legal and technical problems, but if those problems prove solvable we may do it. Can you please describe your exact use case? What parts of JavaFX exactly do you want to embed in a Swing app?

Posted by Josh Marinacci on June 30, 2009 at 11:42 AM PDT #

Josh,

I appreciate your quick answer.

I have electronic trading application - written in Java Swing (for the funny part faster than the C# and C++ equivalent), running on JSE 6.0, where we would like to embed videos (streamed from a server). Ideally, an all-in-one solution, would be ideal for us, ie, to play video or stream video on demand for the customer/trader.

Another use, a fancier one, is the ability to play any type of sound, or music file, in the GUI. It is something that is not easy using the current sound API (due to codecs, encoding, bit rates, etc.).

Also, it would allow to bring nice effects to the existing GUIs - things we are thinking of is the use of JXLayer, etc -- but I guess this is something we can achieve without FX, but it is much more painful.

I do understand that if there is no demand for it, do not do it... but please run a poll. Let's see if there is any interest.

Kind regards

Posted by 86.149.162.73 on June 30, 2009 at 11:54 AM PDT #

Hi Josh,

I also need to use JavaFX on Swing. I was under the impression that you could already do that, although not officially supported, by using JFXtras new JavaFX to Swing component. Am I wrong?

I also was looking to embed video on Modellus so students can analyse the behavior of objects on a video. Things like velocity, acceleration, collision, etc, etc.

Regards.

Posted by Pedro Duque Vieira on July 01, 2009 at 03:02 PM PDT #

Hello,

You can learn here how to add a control bar to the video player : http://blog.lexique-du-net.com/index.php?category/JavaFX

Have a nice day !

Posted by Jmix90 on July 05, 2009 at 12:40 PM PDT #

Still a long way to go to catch Silverlight 3.
Please support JavaFX in Swing.

Posted by 81.158.55.33 on July 10, 2009 at 03:21 PM PDT #

Hello , can any one tell me if Java FX can be used in PHP based website

Posted by Shahab Hameed on July 17, 2009 at 02:02 AM PDT #

i tried doing this but i always get an error when running it on netbeans, it says something like this:

FX Media Object caught Exception com.sun.media.jmc.MediaUnsupportedException

same case as this one thread in a forum i found.
http://forums.sun.com/thread.jspa?threadID=5385035
is there really a bug in the jar file?
if not could somebody help me fix this.

jonathan.lastrilla@gmail.com
kindly waiting for your help.

Posted by jonathan on August 11, 2009 at 07:47 PM PDT #

I want JavaFX in Swing! This is just a obvious idea. Why not allow it and make it possible It'll give Java desktop clients a lot more usability. Really disappointed that Sun didn't see this.

Posted by Edward on November 24, 2009 at 03:32 PM PST #

Post a Comment:
Comments are closed for this entry.