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]
        }
    }   
}