Table of contents
1.
Introduction
2.
Media Events
3.
JavaFX.scene.media.Media class
4.
JavaFX.scene.media.MediaPlayer class
5.
Frequently Asked Questions
5.1.
What is the main purpose of JavaFX?
5.2.
What are various features of JavaFX?
5.3.
What are the controls in JavaFX?
6.
Conclusion
Last Updated: Mar 27, 2024

Introduction to Media with JavaFX

Author Aditya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Rich internet apps for today's world must be able to play and manipulate media files as needed. JavaFX has a media-rich API that can play audio and video at the user's request.

 

 

Users can add audio and video to rich internet applications using the JavaFX Media API (RIAs). The JavaFX media API may distribute media content across a variety of devices, including TVs, smartphones, tablets, and more.

 

Must Recommended Topic, procedure call in compiler design

Media Events

The media API was created by the JavaFX team to be event-driven. To handle media events, use the callback behavior that is associated with the media functions. It is implemented to respond to the media player's OnXXXX events, where XXXX is the event name, rather than writing code for a button via an EventHandler.

 

java.lang.Runnable Callbacks that are activated in response to events are implemented as functional interfaces. We would develop Lambda expressions (java.lang.Runnable interfaces) to be set on the onReady event while playing media material in JavaFX. Consider the case below.

 

  1. Media media = new Media(url);  
  2. MediaPlayer mediaPlayer = new MediaPlayer(media);  
  3. Runnable playMusic = () -> mediaPlayer.play();  
  4. mediaPlayer.setOnReady(playMusic);  

 

 

In the table below, potential media and media-player events are discussed.

 

              Class         Set On Method           Description
Media setOnError() When there is a mistake, this approach is used. It belongs to the Media class.
MediaPlayer setOnError() When there is a mistake, this approach is used.
MediaPlayer setOnHalted() When the media's status changes to "halted," this procedure is used.
MediaPlayer setOnEndOfMedia() When the media play has finished, the method is called.
MediaPlayer setOnMarker() When the Marker event is triggered, this procedure is called.
MediaPlayer setOnPaused() When there is a pause event, this method is called.
MediaPlayer setOnPlaying() Whenever the play event takes place, this method is called.
MediaPlayer setOnReady() When the media is in a ready state, this procedure is called.
MediaPlayer setOnRepeat() When the repeat property is set, this approach is used.
MediaPlayer setOnStalled() When the media player stalls, this procedure is used.
MediaPlayer setOnError() When a media view error occurs, this method is called.

 

JavaFX.scene.media.Media class

The following table lists the class's characteristics. With the exception of onError, all properties are read-only.

 

                        Property                         Description
error When an error happens, this property is set to the media exception value. This property belongs to the MediaException class and is of the object type. 
height the source media's height in pixels. This property has an integer type.
width the pixels that make up the source media's width. This attribute is of the integer type.
onError the pixels that make up the source media's width. This attribute is of the integer type.

 

JavaFX.scene.media.MediaPlayer class

 

             Property               Property         Setter Methods
audioSpectrumListener This belongs to the class AudioSpectrumListener's object type attribute. The AudioSpectrumListener for an audio spectrum is indicated. setAudioSpectrumListener(AudioSpectrumListener listener)
audioSpectrumThreshold This property has an integer type. The sensitivity threshold is indicated. setAudioSpectrumThreshold(int value)
audioSpectrumNumBands This property has an integer type. It shows how many bands there are between the audio spectrum. setAudioSpectrumNumBands(int value)
autoPlay The boolean-type property is this. According to the true value, playback will begin as soon as possible. setAutoPlay(Boolean value)
bufferProgressTime This belongs to the class Duration and is an object type attribute. It displays the maximum amount of time that media can be played before the media player stalls. It cannot be set as it is a read-only property.
balance This property has a double type. It displays the audio output's balance. setBalance(double value)
currentCount This integer type property is read-only. It displays how many finished playback cycles there have been. It cannot be set as it is a read-only property.
currentRate It is a property of the double type. It displays the playback speed as it is right now. The property is read-only. It cannot be set as it is a read-only property.
cycleCount It is a property of the integer type. The number of times the media will be played is shown. setCycleCount(int value)
currentTime This belongs to the class Duration and is an object type attribute. It displays the duration of the current media playback. It cannot be set as it is a read-only property.
cycleDuration It is the read-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. It cannot be set as it is a read-only property.
mute It is a property of the boolean type. It shows whether or not the audio is muted. SetMute(boolean value)
onEndOfMedia It belongs to the Runnable interface as an object type property. When the media file reaches its end, it is set to an Event Handler that will be called. setOnEndOfMedia(java.lang.Runnable value)
onMarker It belongs to the class MediaMarkerEvent as an object type attribute. It identifies the EventHandler that will be called when the media marker is reached in the current time. setOnMarker(EventHandler<MediaMarkerEvent> onMarker )
onPaused It belongs to the Runnable interface as an object type property. It identifies the EventHandler that will be called if the status is changed to stopped. setOnPaused(java.lang.Runnable value)
onPlaying It belongs to the Runnable interface as an object type property. When the status is changed to playing, it denotes the EventHandler that will be called. setOnPlaying(java.lang.Runnable value)
onRepeat The MediaMarkerEvent class's object type property belongs to it. It designates the EventHandler that will be called once and repeat once the current time approaches the stop time. setOnRepeat(java.lang.Runnable value)

 

Frequently Asked Questions

What is the main purpose of JavaFX?

Developers can construct rich client applications that work reliably across a range of platforms by designing, building, testing, debugging, and deploying them using JavaFX, a collection of graphics and media packages.

What are various features of JavaFX?

FXML mark-up produced by Scene Builder can be imported into an IDE. JavaFX applications can be integrated into web pages. WebKitHTML technology is used by Web View to embed web pages. There are built-in components in JavaFX that are independent of the operating system.

What are the controls in JavaFX?

In a JavaFX application, JavaFX controls are JavaFX components that offer some sort of control capability. A button, radio button, table, tree, etc. are a few examples. A control must be associated to the scene graph of some Scene object in order for it to be displayed.

 

Conclusion

In this article, we see media with JavaFX and set of events with JavaFX, we had to also seen the set of properties and method with media events.

We hope this blog has helped you enhance your knowledge regarding Introduction to Media with JavaFX

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, and System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass