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.

  1. javafx.scene.media.Media
  2. javafx.scene.media.MediaPlayer
  3. javafx.scene.media.MediaStatus
  4. 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.

ClassSet On MethodDescription
MediasetOnError()This method is invoked when an error occurs. It is the part of the class Media.
MediaPlayersetOnEndOfMedia()The method is invoked when end of the media play is reached.
MediaPlayersetOnError()This method is invoked when an error occurs.
MediaPlayersetOnHalted()This method is invoked when the status of media changes to halted.
MediaPlayersetOnMarker()This method is invoked when the Marker event is triggered.
MediaPlayersetOnPaused()This method is invoked when a pause event occurs.
MediaPlayersetOnPlaying()This method is invoked when the play event occurs.
MediaPlayersetOnReady()This method is invoked when the media is in ready state.
MediaPlayersetOnRepeat()This method is invoked when the repeat property is set.
MediaPlayersetOnStalled()This method is invoked when the media player is stalled.
MediaPlayersetOnStopped()This method is invoked when the media player has stopped.
MediaViewsetOnError()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.

PropertyDescription
durationThe duration of the source media in seconds. This property is of object type of the class Duration.
errorThis is a property set to media exception value when an error occurs. This property is of the type object of the class MediaException.
heightThe height of the source media in pixels. This is an integer type property.
onErrorThe event handler which is called when the error occurs. The method setOnError() is used to set this property.
widthThe 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.

PropertyPropertySetter Methods
audioSpectrumIntervalThis is a double type property. It indicates the interval between the spectrum updates in seconds.setAudioSpectrumInterval (double value)
audioSpectrumListenerThis is an object type property of the class AudioSpectrumListener. It indicates the audiospectrumlistener for an audio spectrum.setAudioSpectrumListener(AudioSpectrumListener listener)
audioSpectrumNumBandsThis is an integer type property. It indicates the number of bands between the audio spectrum.setAudioSpectrumNumBands(int value)
audioSpectrumThresholdThis is an integer type property. It indicates the sensitivity thresholdsetAudioSpectrumThreshold(int value)
autoPlayThis is the boolean type property. The true value indicates the playing will be started as soon as possible.setAutoPlay(Boolean value)
balanceThis is a double type property. It indicates the balance of the audio output.setBalance(double value)
bufferProgressTimeThis 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.
currentCountThis is read only integer type property. It indicates the number of completed playback cycles.Can not be set as it is read only property.
currentRateThis 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.
currentTimeThis 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.
cycleCountIt is the integer type property. It indicates the number of times, the media will be played.setCycleCount(int value)
cycleDurationIt 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.
errorIt 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.
muteIt is a boolean type property. It indicates whether the audio is muted or not.SetMute(boolean value)
onEndOfMediaIt 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)
onErrorIt 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)
onMarkerIt 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 )
onPausedIt 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)
onPlayingIt 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)
onReadyIt 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)
onRepeatIt 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)
onStalledIt 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)
onStoppedIt 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)
rateIt is the double type property. It indicates the rate at which the media should be played.setRate(double value)
startTimeThis property is of the type object of the class Duration. It indicates the time where media should start playing.setStartTime(Duration value)
statusThis is the read only property. It indicates the current state of the Media player.Can not be set as it is read only property.
stopTimeThis property is an object type of the class Duration. It indicates the time offset where the media should stop playing.setStopTime(double value)
totalDurationIt 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.
volumeIt 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.






Latest Courses