Media with JavaFX
Modern world’s rich internet applications must be capable to play and edit the media files when required. JavaFX provides the media-rich API that can play audio and video on the user’s demand.
JavaFX Media API enables the users to incorporate audio and video into the rich internet applications (RIAs). JavaFX media API can distribute the media content across the different range of devices like TV, Mobile, Tablets and many more.
In this part of the tutorial, we will discuss the capability of JavaFX to deal with the media files in an interactive way. For this purpose, JavaFX provides the package javafx.scene.media that contains all the necessary classes. javafx.scene.media contains the following classes.
- javafx.scene.media.Media
- javafx.scene.media.MediaPlayer
- javafx.scene.media.MediaStatus
- javafx.scene.media.MediaView
Media Events
The JavaFX team have designed media API to be event driven. The callback behaviour attached with the media functions are used to handle media events. Instead of typing code for a button via a EventHandler, a code is implemented that responds to the triggering of the media player’s OnXXXX events where XXXX is the event name.
java.lang.Runnable functional interfaces are used as the callbacks which are invoked when an event is encountered. When playing the media content in javafx, we would create the Lambda expressions (java.lang.Runnable interfaces) to be set on the onReady event. Consider the following example.
The playMusic variable is assigned to a lambda expression. This get passed into the Media player’s setOnReady() method. The Lambda expression will get invoked when the onReady event is encountered.
Possible media and media-player events are discussed in the following table.
Class | Set On Method | Description |
---|---|---|
Media | setOnError() | This method is invoked when an error occurs. It is the part of the class Media. |
MediaPlayer | setOnEndOfMedia() | The method is invoked when end of the media play is reached. |
MediaPlayer | setOnError() | This method is invoked when an error occurs. |
MediaPlayer | setOnHalted() | This method is invoked when the status of media changes to halted. |
MediaPlayer | setOnMarker() | This method is invoked when the Marker event is triggered. |
MediaPlayer | setOnPaused() | This method is invoked when a pause event occurs. |
MediaPlayer | setOnPlaying() | This method is invoked when the play event occurs. |
MediaPlayer | setOnReady() | This method is invoked when the media is in ready state. |
MediaPlayer | setOnRepeat() | This method is invoked when the repeat property is set. |
MediaPlayer | setOnStalled() | This method is invoked when the media player is stalled. |
MediaPlayer | setOnStopped() | This method is invoked when the media player has stopped. |
MediaView | setOnError() | This method is invoked when an error occurs in the media view. |
We must notice that MediaPlayer class contains the most number of events triggered while MediaView and Media classes contains one event each.
javafx.scene.media.Media class
The properties of the class are described in the following table. All the properties are the read only except onError.
Property | Description |
---|---|
duration | The duration of the source media in seconds. This property is of object type of the class Duration. |
error | This is a property set to media exception value when an error occurs. This property is of the type object of the class MediaException. |
height | The height of the source media in pixels. This is an integer type property. |
onError | The event handler which is called when the error occurs. The method setOnError() is used to set this property. |
width | The width of the source media in pixels. This is an integer type property |
Constructors
There is a single constructor in the table.
public Media(java.lang.String source): it instantiate the class Media with the specified source file.
JavaFX.scene.media.MediaPlayer class
The properties of the class along with the setter methods are described in the following table.
Property | Property | Setter Methods |
---|---|---|
audioSpectrumInterval | This is a double type property. It indicates the interval between the spectrum updates in seconds. | setAudioSpectrumInterval (double value) |
audioSpectrumListener | This is an object type property of the class AudioSpectrumListener. It indicates the audiospectrumlistener for an audio spectrum. | setAudioSpectrumListener(AudioSpectrumListener listener) |
audioSpectrumNumBands | This is an integer type property. It indicates the number of bands between the audio spectrum. | setAudioSpectrumNumBands(int value) |
audioSpectrumThreshold | This is an integer type property. It indicates the sensitivity threshold | setAudioSpectrumThreshold(int value) |
autoPlay | This is the boolean type property. The true value indicates the playing will be started as soon as possible. | setAutoPlay(Boolean value) |
balance | This is a double type property. It indicates the balance of the audio output. | setBalance(double value) |
bufferProgressTime | This is an object type property of the class Duration. It indicates the duration of the media which can be played without stalling the media-player. | Can not be set as it is read only property. |
currentCount | This is read only integer type property. It indicates the number of completed playback cycles. | Can not be set as it is read only property. |
currentRate | This is a double type property. It indicates the current rate of the playback. It is read only property. | Can not be set as it is read only property. |
currentTime | This is an object type property of the class Duration. It indicates the current media playback time. | Can not be set as it is read only property. |
cycleCount | It is the integer type property. It indicates the number of times, the media will be played. | setCycleCount(int value) |
cycleDuration | It is the ready only property. It is of the type object of the class Duration. It indicates the amount of time between the start time and stop time of the media. | Can not be set as it is read only property. |
error | It is a read only property. It is an object type property of the class MediaException. It is set to a Media-Exception if an error occurs. | Can not be set as it is read only property. |
mute | It is a boolean type property. It indicates whether the audio is muted or not. | SetMute(boolean value) |
onEndOfMedia | It is an object type property of the interface Runnable. It is set to an Event Handler which will be invoked when the end of the media file is reached. | setOnEndOfMedia(java.lang.Runnable value) |
onError | It is an object type property of the interface Runnable. It indicates the Event Handler which will be invoked when the status changes to halted. | setOnHalted(java.lang.Runnable value) |
onMarker | It is an object type property of the class MediaMarkerEvent. It indicates the EventHandler which will be invoked when the current time reaches the media marker. | setOnMarker(EventHandler<MediaMarkerEvent> onMarker ) |
onPaused | It is an object type property of the interface Runnable. It indicates the EventHandler which will be invoked when the status changed to paused. | setOnPaused(java.lang.Runnable value) |
onPlaying | It is an object type property of the interface Runnable. It indicates the EventHandler which will be invoked when the status changed to playing. | setOnPlaying(java.lang.Runnable value) |
onReady | It is an object type property of the interface Runnable. It indicates the EventHandler which will be invoked when the status changed to Ready. | setOnReady(java.lang.Runnable value) |
onRepeat | It is an object type property of the class MediaMarkerEvent. It indicates the EventHandler which will be invoked when the current time reaches the stop time and will be repeating. | setOnRepeat(java.lang.Runnable value) |
onStalled | It is an object type property of the interface Runnable. It indicates the Event Handler which will be invoked when the status changed to Stalled. | setOnStalled(java.lang.Runnable value) |
onStopped | It is an object type property of the interface Runnable. It indicates the EventHandler which will be invoked when the status changed to Stopped. | setOnStopped(java.lang.Runnable value) |
rate | It is the double type property. It indicates the rate at which the media should be played. | setRate(double value) |
startTime | This property is of the type object of the class Duration. It indicates the time where media should start playing. | setStartTime(Duration value) |
status | This is the read only property. It indicates the current state of the Media player. | Can not be set as it is read only property. |
stopTime | This property is an object type of the class Duration. It indicates the time offset where the media should stop playing. | setStopTime(double value) |
totalDuration | It is an object type property of the class Duration. It indicates the total time during which the media should be played. | Can not be set as it is read only property. |
volume | It is a double type property. It indicates the volume at which the media should be playing. | setVolume(double value) |
Constructors
The class contains only a single constructor which is given below.