Java Reference
In-Depth Information
Getting ready
This recipe uses classes from the
Media API
located in the
javafx.scene.media
package.
As mentioned in the introduction of this recipe, the example presented here extends the code
from the previous recipe to transform the audio player to now play video. We are going to
reuse the same icons and the same logic to control the playback of the video. To review how
to configure and use the Media API for playback, review the previous recipe
Playing audio
with MediaPlayer
.
To illustrate video playback, the application plays back the award-winning, open-sourced,
short, animated movie
Big Buck Bunny
. By default, the recipe will play the 854 x 480
H.264 version found at the address
http://mirror.bigbuckbunny.de/peach/
bigbuckbunny_movies/big_buck_bunny_480p_h264.mov
.
How to do it...
Similar to audio, playing video is simple. The abbreviated code given next highlights the
portion of the code that is changed to be able to display video. You can see the full listing
of the code at
ch05/source-code/src/media/VideoPlayerDemo.fx
.
def w = 800;
def h = 600;
def maxW = w * 0.8;
def maxH = h * 0.7;
var scene:Scene;
def
mediaSource
=
"http://mirror.bigbuckbunny.de/peach/bigbuckbunny_movies/big_buck_
bunny_480p_h264.mov";
def
player
=
MediaView
{
layoutX:(w - maxW)/2 layoutY:(h-maxH)/2
mediaPlayer:
MediaPlayer
{media:
Media
{source:
mediaSource
}}
fitWidth:maxW fitHeight:maxH
}
def controls = Group {
layoutX:(w-110)/2
layoutY:h-100
effect:Reflection{
fraction:0.4 bottomOpacity:0.1 topOffset:3}
content:[
HBox{spacing:10 content:[
ImageView
{id:"playCtrl"
image:Image{url:"{__DIR__}play-large.png"}
onMouseClicked:function(e:MouseEvent){
def playCtrl = e.source as ImageView;




