Working with the MediaPlayer and Media classes

The MediaPlayer class is the class which can play media, either audio or video. It doesn't have any UI controls and for the video playback you need to use the corresponding MediaView class (see the next section).

As you don't need any UI for an audio, let's look at it first. Playing an MP3 song looks as follows:

Media media = new Media("http://www.sample-videos.com/audio/mp3/wave.mp3");MediaPlayer mp = new MediaPlayer(media);mp.play();

You can control playback in various ways using Media class properties, APIs, and event handlers; some of them are very similar to Timeline ones (see Chapter 5, Animation). I'll list the most interesting ones:

  • seek (Duration seekTime): Sets the playback to the ...

Get Mastering JavaFX 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.