JavaFX Video - Will It Be Able To Compete With Flash?

Yesterday, in a “break” from a lovely weekend filled with work (oh joy!), I decided to see if I could find an answer to a question about JavaFX that I’ve been wondering about for a while.  What was the question?  It’s this: will JavaFX be able to compete with Flash when it comes to rich media in general, and video in particular?

As you may know, Flash revolutionized video on the web.  It enabled developers and designers to build compelling, custom video players - unique to their own companies - that “just work”.   Prior to using Flash, video playback on the web was pretty clunky, using Windows Media Player or Real Player, which often didn’t seem to work. There are a few key ingredients in Flash that went to making that possible:

  1. A built-in, cross-platform video codec (if you’ve got Flash in your browser, you’ve got the Flash codec)
  2. Easy to develop a custom piece of software that plays video
  3. The ability to rapidly build elegant UIs that integrate with the video and make the player easy to use. For example transparent, potentially animated, controls displayed on top of the video, such as a message saying, “Click to play”.

Where does JavaFX stand with regard to the above?  Well, in terms of point #1, while the JavaFX Preview SDK doesn’t have built-in cross-platform codecs, version 1.0 will (the preview SDK uses codecs that are native to the computer you’re running the app on).  So, we can tick that box.

Points #2 and #3 go to the core of the platform, and can be evaluated with the Preview SDK.   To find an answer to my question, I set about building a toy JavaFX app that could play video, and have a fancy animated, transparent graphical doodle running on top of the video.   I was pleased to find that building this toy app took me just five minutes in total - and, I can report that it worked great.  It took a handful of lines of JavaFX code (around thirty lines, pretty much cut and pasted from an example app I found on the Web) to create a super-simple video player (click a button to play the video); with a handful of lines of code to create a pretty fancy transparent doodle running on top of the video ( around 20 lines).

Based on what I’ve seen so far, it looks to me that it will be possible to build some pretty compelling video players, pretty easily, by using JavaFX.   So, to answer my original question - will JavaFX be able to compete with Flash, when it comes to video?   I’d say it’s most certainly headed in the right direction…

Update: As per Sam’s request, here’s the code (and a screen shot) - yes, I’m sure I’m doing lots of things sub-optimally.  I’m only a beginner with JavaFX.

Screenshot from toy app
/*
 * Main.fx
 *
 * Created on 02-Aug-2008, 12:29:32
 */

package toyplayer;

import javafx.ext.swing.*;
import javafx.scene.paint.*;
import javafx.scene.geometry.*;
import javafx.scene.media.*;
import javafx.scene.text.*;
import javafx.scene.*;
import java.lang.*;
import javafx.input.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.geometry.*;
import javafx.application.*;
import javafx.scene.transform.*;
import javafx.input.*;
import javafx.animation.*;
import java.lang.System;

var angle = 0.0;

/**
 * A toy application to see if transparent animated
 * graphics can be rendered on top of a video
 * It's heavily based on bits of code
 * gathered from the Web. Sorry for no attribution,
 * I wasn't intending to make this code public
 * - particularly as my code is almost certainly
 *  not very well written!
 *
 * @author Simon Brocklehurst
 */
public class ToyVideoPlayer {

   //hard-coded reference to a video
   //on my hard-drive.
   //The one I'm playing is an mpeg4
   //of Diggnation from Revision3

   //Works with audio files as well as video

    private attribute mediaURL = "C://test.mp4";

    private attribute player:MediaPlayer = MediaPlayer {

        media: Media {
            source: mediaURL;
        }

        autoPlay: false
        repeatCount: MediaPlayer.REPEAT_FOREVER
    }

    attribute frame:SwingFrame = SwingFrame {

        content: Canvas {
            content: [
                MediaView {
                    mediaPlayer: player
                },

                Group {
                    content: [
                        Rectangle {
                            width: 100 height: 40 arcHeight: 20 arcWidth: 20;
                            fill: Color.rgb(0,0,0, 0.4);
                            onMousePressed: function(e:MouseEvent):Void {
                                player.play();
                            }
                        },

                        Text {
                            content: "Click to Play";
                            fill: Color.WHITE;
                            translateX: 15;
                            translateY: 25;
                        },
                        for(i in [0..10]) {
                            // Binding
                            Rectangle {
                                fill: Color.rgb(255,25 * i,0, i / 10.0);
                                width: 40 height: 10 arcHeight: 10 arcWidth: 10;
                                stroke: Color.BLACK strokeWidth: 1;
                                translateX: 150;
                                transform: bind [
                                    Transform.rotate(-i * 36 + angle / 2,30,30),
                                    Transform.translate(angle / 8,0),
                                ];
                            }
                        }
                    ]
                    translateX: 110
                    translateY: 190
                }
            ]
        }

        visible: true

        closeAction: function() {
            player.pause();
            frame.close();
            System.exit(0);
        }
        title: "Toy Video Player - Close the window to quit"
    }
}

var anim = Timeline { keyFrames: [
        KeyFrame { time: 0s values:
                angle => -360 tween Interpolator.EASEBOTH },
        KeyFrame { time: 2.5s values:
                angle => 360 tween Interpolator.EASEBOTH },
    ]
    autoReverse: true
    repeatCount: Timeline.INDEFINITE
};
anim.start();

var app:ToyVideoPlayer = ToyVideoPlayer{}

That’s it…

Trackbacks & Pings

  1. alldevnet.com on 04 Aug 2008 at 2:50 pm

    JavaFX Video - Will It Be Able To Compete With Flash?…

    This blog post compares the new JavaFX video with Flash that revolutionized video on the web….

Comments

  1. Sam wrote:

    Can you post your toy app?

  2. simon wrote:

    The JavaFX source code is now in the post.

  3. JOKe wrote:

    Doesnt work for me :( i dont know why
    it aways say :
    FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: C://Test.mp4
    source =’C://Test.mp4′

    but i have file called Test.mp4 ( yes with uppercase ) i dont know i try with different files i aways get this.

    Other issue is that when i have var declared before the class like in the example the class dont see it … i have no idea why .
    Im using javafx1.0pre1 with java 1.6 update N build 25 and im starting this in netbeans 6.1 with javaFX plugin from the beta repository

  4. simon wrote:

    @JOKe, I think that error message means that the system can’t find the file.

    Just to check - have you placed your Test.mp4 in the root directory of your C: drive? That is, in C:\\Test.mp4 (in JavaFX, this is specified as C://Test.mp4).

  5. JOKe wrote:

    about the last message i try c:// c:\\ c:/ and always i get this exception

  6. simon wrote:

    OK - I’ve just done a test - that exception also occurs if the system can’t play the video file (I named a random non-video file test.mp4).

    Can a native player (e.g. QuickTime) play the mp4 file you’re using on your computer? Have you tried an audio file (e.g. an mp3)?

  7. Maris wrote:

    I got the same exception

    FX Media Object caught Exception com.sun.media.jmc.MediaUnavailableException: Media unavailable: C://test.mp4
    source =’C://test.mp4′

    If I double click on the file a native players opens it and can play it.

  8. simon wrote:

    Hmmm… that’s odd. Have you tried any other formats of media - e.g. audio mp3, windows wmv format etc.

    For information, in case it’s relevant….

    I run this example on Vista, and that particular mp4 file runs in QuickTime on my system.

    The particular type of mpeg4 file I used was one I download from Revision3 (what they call a large Quicktime format file).

  9. Babatunde Adeyemi wrote:

    Are you really sure? Only time will tell, cos if what Kirill (http://www.pushing-pixels.org/?p=393) says is true, we need much more than that from JavaFX

  10. simon wrote:

    Can you clarify what you mean by “we need much more than that”? That is - “much more than” what, exactly?

    The post you cite is simply a tutorial about how to use the new JMC library in a Swing application to play videos encoded with a variety of codecs. It doesn’t appear to say anything about what the JavaFX platform might need, in order to be successful in Internet video.

  11. Babatunde Adeyemi wrote:

    Ok sorry. Kirill gave a tutorial on how to use the new JMC, though he didn’t make mention of the problems with using it, but in the reply to one of the comments to his post, he now made us realize that you have to have a good bunch of codecs installed to be able to use it. This is the actual section I was referring to: http://www.pushing-pixels.org/?p=393#comment-3794 and http://www.pushing-pixels.org/?p=393#comment-3795

  12. Babatunde Adeyemi wrote:

    As regards clarifying what I mean by we need much more than that, I mean that we need to do more than build applications that rely on the native codecs installed on the end-user’s system

  13. Babatunde Adeyemi wrote:

    Though the current one is just a preview candidate, I hope that by the time the version 1.0 comes out, we’d be proud to be Java developers once again by getting JavaFX right. Sun promises to make what we really need available by then, according to http://www.javafx.com/htdocs/whatscoming.html

  14. simon wrote:

    Oh yes, that’s right. That’s what I said point #1 in the blog - this issue is taken care of. JavaFX V1.0 will come with at least one built-in cross-platform video codec. So, there will be zero reliance on codecs installed on end-user’s systems for delivering video over the web. One of these codecs will be ON2 TrueMotion VP6 (which is, I think, the same codec that Flash uses).

  15. JOKe wrote:

    I still cannot play mp4 file I have quicktime alternative so i played with media player but I cant play any files not only mp4 i cant play avi too
    I read in this blog : http://www.pushing-pixels.org/?p=393 - ” Add the lib/jmc.jar to the classpath of your project ” maybe this is the problem ?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*

*