📜  JavaFX 多媒体

📅  最后修改于: 2020-10-14 07:26:10             🧑  作者: Mango

JavaFX的媒体

需要时,现代世界的富Internet应用程序必须能够播放和编辑媒体文件。 JavaFX提供了丰富的媒体API,可以根据用户需求播放音频和视频。

JavaFX Media API使用户可以将音频和视频合并到富互联网应用程序(RIA)中。 JavaFX媒体API可以在电视,移动设备,平板电脑等不同设备上分发媒体内容。

在本教程的这一部分中,我们将讨论JavaFX以交互方式处理媒体文件的功能。为此,JavaFX提供了包含所有必需类的包javafx.scene.media。 javafx.scene.media包含以下类。

  • javafx.scene.media.Media
  • javafx.scene.media.MediaPlayer
  • javafx.scene.media.MediaStatus
  • javafx.scene.media.MediaView

媒体活动

JavaFX团队已将媒体API设计为事件驱动的。媒体函数附带的回调行为用于处理媒体事件。代替通过EventHandler为按钮键入代码,而是实现了一种代码,该代码响应媒体播放器的OnXXXX事件的触发,其中XXXX是事件名称。

java.lang.Runnable功能接口用作遇到事件时调用的回调。在javafx中播放媒体内容时,我们将创建要在onReady事件上设置的Lambda表达式(java.lang.Runnable接口)。考虑以下示例。

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

playMusic变量被分配给lambda表达式。这被传递到媒体播放器的setOnReady()方法中。遇到onReady事件时,将调用Lambda表达式。

下表讨论了可能的媒体和媒体播放器事件。

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.

我们必须注意,MediaPlayer类包含最多触发的事件,而MediaView和Media类各自包含一个事件。

javafx.scene.media.Media类

下表描述了该类的属性。除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

建设者

表中只有一个构造函数。

public Media(java.lang.String source):它使用指定的源文件实例化Media类。

JavaFX.scene.media.MediaPlayer类

下表描述了该类的属性以及setter方法。

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 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)

建设者

该类仅包含一个构造函数,如下所示。

public MediaPlayer (Media media)