Multimedia‎ > ‎

A Video Player


Playing video in JavaFX is just as easy as playing audio using the JavaFX Media API located in the javafx.scene.media package.  The API offers three classes that participate in video playback
  • Media - loads or downloads (from remote location) the media source
  • MediaPlayer - plays back the media source and provides control over playback cycle 
  • MediView - a scene graph node that renders the video on the screen
The following example shows how to playback a video stream from the web:

def w = 800;
def h = 600;
def maxW = w * 0.8;
def maxH = h * 0.7;

def mediaSource = "http://mirror.bigbuckbunny.de/peach/bigbuckbunny_movies/big_buck_bunny_480p_h264.mov";

def player = MediaView{
    mediaPlayer:MediaPlayer {
        media:Media{
            source:mediaSource
        }
    }

    layoutX:(w - maxW)/2 
    layoutY:(h-maxH)/2
    fitWidth:maxW 
    fitHeight:maxH
}

player.mediaPlayer.play();

When the code runs, it will start streaming the video assigned to variable mediaSource as shown below.


The materials on this website represent a small sample of content loosely based on the book JavaFX Application Development Cookbook.  The book offers far more content with over 80 recipes covering a range of topics from basics to advanced concepts.  

Buy the Book

You can get your copy of the book directly from the publisher. Click here to order!