”;
In general, media refers to various means of communication such as audio, video, and other forms that can be both one-way or two-way. It is used in the form of music, movies, news, vlogs and so forth.
Since media usage became an essential part of our lives, JavaFX creators developed a set of Java APIs that enables the use of media within JavaFX applications.
Handling Media in JavaFX
To handle media, JavaFX provides javafx.scene.media package that allows developers to embed a media player within the desktop window or a web page on supported platforms for media playback. This package contains the following classes that are used in combination to handle media content −
-
Media − It contains pieces of information like source, resolution, and metadata of a specified media resource.
-
MediaPlayer − It provides the controls for playing media.
-
MediaView − It is a node object used to provide support for animation and other effects to the media resource.
The classes mentioned above are not independent, they are always used in the combination to create an embedded media player. All attributes and methods needed to control media playback are made available in MediaPlayer class. More specifically, the play(), stop(), and pause() methods are used to control media playback. To adjust the volume level, we use VOLUME variable. The range of volume level is from 0 to 1.0 (the maximum value).
Remember, without the MediaView class, we can”t view the media being played by media player.
There are some other additional methods that are used to handle the following events −
-
Buffers data
-
Whenever any errors of the MediaErrorEvent class occur.
-
When media stops because media player has not received data fast enough to continue playing.
-
When the media player reaches the end of media.
Supported format of Media in JavaFX
The following media formats are supported in JavaFX −
S.No | Media & Format |
---|---|
1 |
Audio
MP3, AIFF , WAV, and MPEG-4
|
2 |
Video
FLV containing VP6 video and MP3 audio, and MPEG-4
|
Features provided by JavaFX media
The following features are provided by JavaFX media on devices that support JavaFX −
-
It supports multiple playback functions, such as Play, Pause, Stop, Volume, and Mute.
-
It allows navigation through audio/video in a forward or backward direction as needed.
-
It provides support for HTTP and File protocols.
-
Progressive download
-
It also supports HTTP live streaming.
Handling Media Reference
Following are the classes and methods that are used for handling media in JavaFX.
Media
The Media class in the JavaFx is like a container for audio video files. It contains pieces of information like source, resolution, and metadata of a specified media resource. Some of the methods of the ”Media” class listed below are −
Sr.No. | Methods & Description |
---|---|
1 | getDuration()
Retrieve the duration in seconds of the media. |
2 | getWidth()
Retrieve the width in pixels of the media. |
3 | getHeight()
Retrieve the height in pixels of the media. |
4 | getSource()
Retrieve the source URI of the media. |
5 | getError()
Return any error encountered in the media. |
MediaPlayer
The MediaPlayer class controls audio and video files and allows for functions like play, pause, and stop, making media control easy. Some of the methods of the ”MediaPlayer” class listed below are −
Sr.No. | Methods & Description |
---|---|
1 | getStatus()
Retrieves the current player status. |
2 | getVolume()
Retrieves the audio playback volume. |
3 | setRate()
Sets the playback rate to the supplied value. |
4 | isAutoPlay()
Retrieves the autoPlay property value. |
5 | getBlance()
Retrieves the audio balance. |
6 | setMute()
Sets the value of muteProperty(). |
7 | getStartTime()
Retrieves the start time. |
8 | getStopTime()
Retrieves the stop time. |
9 | getTotalDuration()
Retrieves the total playback duration including all cycles (repetitions). |
10 | stop()
Stops playing the media. |
MediaView
The MediaView class in JavaFX is a special class for displaying videos or media played by a MediaPlayer. Some of the methods of the ”MediaView” class listed below are −
Sr.No. | Methods & Description |
---|---|
1 | getX()
Retrieves the x coordinate of the MediaView origin. |
2 | getY()
Retrieves the y coordinate of the MediaView origin. |
3 | setSmooth()
Sets whether to smooth the media when scaling. |
4 | getViewPort()
Retrieves the rectangular viewport into the media frame. |
5 | isPreserveRatio()
Sets whether to preserve the media aspect ratio when scaling. |
6 | getFitHeight()
Retrieves the height of the bounding box of the resized media. |
7 | getFitWidth()
Retrieves the width of the bounding box of the resized media. |
”;