MPEG-4 multimedia support in JavaFX

Content by Brian Burkhalter 

With JavaFX 2.1, we are introducing playback support for digital media stored in the MPEG-4 multimedia container format containing H.264/AVC video and Advanced Audio Coding (AAC) audio. This new capability will work across all operating systems supported by JavaFX, including Mac OS X, Linux, 32-bit Windows XP and Vista, and 32-bit and 64-bit Windows 7. On Mac OS X and Windows 7 playback will be functional without requiring additional software, however on Linux and versions of Windows older than Windows 7, readily available third party packages (such as this one) must be acquired and installed on the system.

Actual audio and video decoding rely on operating system-specific media engines. The JavaFX media framework does not however attempt to handle all multimedia container formats and media encodings supported by these native engines, opting instead so far as possible to provide equivalent, well-tested functionality across all platforms on which JavaFX runs.

One known limitation of JavaFX MPEG-4 media playback on Mac OS X is that at most one single H.264-encoded video stream may be played at a time. There are however no inherent limitations on the number of AAC-encoded tracks which may be played aside from those imposed by system resources. This issue is still under investigation.


Playing an MPEG-4 media source is identical to playing any other type of media content*. The media stack dynamically detects the type of multimedia container and encodings and responds accordingly.

Here is what the code looks like:

/*

 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 */

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.collections.MapChangeListener;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.media.Media;
import javafx.scene.media.MediaView;
import javafx.scene.media.Track;
import javafx.stage.Stage;

/**
 * A sample media player which loops indefinitely over the same video
 */
public class MediaPlayer extends Application {
private static final String MEDIA_URL = "http://someserver/somedir/somefile.mp4";

private static String arg1;

    @Override public void start(Stage stage) {
        stage.setTitle("Media Player");

// Create media player
        Media media = new Media((arg1 != null) ? arg1 : MEDIA_URL);
        javafx.scene.media.MediaPlayer mediaPlayer = new javafx.scene.media.MediaPlayer(media);
        mediaPlayer.setAutoPlay(true);
        mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE);

// Print track and metadata information
        media.getTracks().addListener(new ListChangeListener<Track>() {
public void onChanged(Change<? extends Track> change) {
                System.out.println("Track> "+change.getList());
            }
        });
        media.getMetadata().addListener(new MapChangeListener<String,Object>() {
public void onChanged(MapChangeListener.Change<? extends String, ? extends Object> change) {
                System.out.println("Metadata> "+change.getKey()+" -> "+change.getValueAdded());
            }
        });

// Add media display node to the scene graph
        MediaView mediaView = new MediaView(mediaPlayer);
        Group root = new Group();
        Scene scene = new Scene(root,800,600);
        root.getChildren().add(mediaView);
        stage.setScene(scene);
        stage.show();
    }

public static void main(String[] args) {
if (args.length > 0) {
            arg1 = args[0];
        }
        Application.launch(args);
    }
}

(*) JavaFX 2.0 already supports  the following media formats:

  • Audio: MP3; AIFF containing uncompressed PCM; WAV containing uncompressed PCM
  • Video: FLV containing VP6 video and MP3 audio

Comments:

how can i find out programmatically if a particular system supports h264 playback?

Posted by guest on February 09, 2012 at 09:01 PM PST #

Is there any intention to integrate WebM (VP8+Vorbis) or Ogg Theora support into JavaFX?

Posted by guest on February 10, 2012 at 07:57 AM PST #

Although nothing should prevent JavaFX to support WebM from a technical point of view, I cannot comment on Oracle's plans to support it due to legal issues with a certain company...

There are no plans to support Ogg Theora as part of JavaFX. I will not go into a discussion on which video and audio formats are technically superior, but let's say that the requirements we have gathered from corporate users don't mention Ogg Theora.

This being said, JavaFX is in the process of being open sourced, and Since the JavaFX media framework is based on GStreamer (another open source technology), WebM and Ogg Theora advocates should have no problems integrating these codecs with OpenJFX.

Posted by nlorain on February 10, 2012 at 09:15 AM PST #

Q: how can i find out programmatically if a particular system supports h264 playback?

A: We do not have an interface at this time to return a list of supported formats and encodings

Posted by nlorain on February 10, 2012 at 03:00 PM PST #

At last!!!!

Posted by guest on February 13, 2012 at 01:31 PM PST #

Brian, this is really an interesting post, thanks! I am actually trying to play (without success) a local mpeg-4 H.264-encoded movie on Mac OS X. I am using substantially the same code you described using javafx-sdk2.1.0-beta12, when do you plan to release the updated (beta) library?

Bests,
Davide

Posted by dmerico on February 14, 2012 at 08:30 AM PST #

Would you be able play a live h264 or MP4 stream via RTSP URL (e.g. rtsp://<cameraIP>:<port>)?

Posted by guest on February 14, 2012 at 11:30 AM PST #

No, we do not support the RTSP protocol. However, we are looking into the possibility to play a live MP4 stream, but not in the near future

Posted by nlorain on February 14, 2012 at 01:30 PM PST #

Regarding Linux compatibility, In the release notes it says 'however on Linux and versions of Windows older than Windows 7, readily available third party packages (such as this one) must be acquired and installed on the system'.

But 'this one' links to a site providing windows only codecs, at least I didn't find suitable codecs for linux there.

is there also some readily available codec for Linux?

Posted by guest on January 10, 2013 at 11:13 PM PST #

For Linux, see the following note from the Systems Requirements (http://docs.oracle.com/javafx/2/system_requirements_2-2-4/jfxpub-system_requirements_2-2-4.htm):

-------
For Linux platform, install the following: libavcodec52 and libavformat52 on Ubuntu Linux 10.04, 10.10, 11.04 or equivalent, or libavcodec53 and libavformat53 on Ubuntu Linux 11.10, 12.04 or equivalent. (Encoding type: AAC audio, MP3 audio, H.264 video, and HTTP Live Streaming.) Note that installing libavformat automatically causes libavcodec to be installed.
-------

Posted by Nicolaas Lorain on January 11, 2013 at 02:49 PM PST #

As far as I understand it's not possible to access individual decoded video frames, e.g. as buffered images.

This is quite a showstopper for any media rich application which goes beyond simple movie playback.

Any future directions for improving video support in JavaFX? Would be great for the whole Java platform, as video support has always been one of the weaknesses.

Posted by guest on February 14, 2013 at 05:55 PM PST #

Please submit a feature request on Jira (http://javafx-jira.kenai.com), so that it can be taken into account for a future release.

You may also want to visit the OTN JavaFX forum (https://forums.oracle.com/forums/forum.jspa?forumID=1385) and ask whether anybody as aware of a workaround.

Posted by guest on February 15, 2013 at 10:16 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blog is maintained by Nicolas Lorain, Java Client Product Manager. The views expressed on this blog are my own & do not necessarily reflect the views of Oracle.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today