public final class MediaPlayer
extends java.lang.Object
MediaPlayer
class provides the controls for playing media.
It is used in combination with the Media
and MediaView
classes to display and control media playback. MediaPlayer
does
not contain any visual elements so must be used with the MediaView
class to view any video track which may be present.
MediaPlayer
provides the pause()
, play()
,
stop()
and seek()
controls as
well as the rate
and autoPlay
properties which apply to all types of media. It also provides the
balance
, mute
, and
volume
properties which control audio playback
characteristics. Further control over audio quality may be attained via the
AudioEqualizer
associated with the player. Frequency descriptors of
audio playback may be observed by registering an AudioSpectrumListener
.
Information about playback position, rate, and buffering may be obtained from
the currentTime
,
currentRate
, and
bufferProgressTime
properties, respectively. Media marker notifications are received by an event
handler registered as the onMarker
property.
For finite duration media, playback may be positioned at any point in time
between 0.0
and the duration of the media. MediaPlayer
refines this definition by adding the startTime
and
stopTime
properties which in effect define a virtual media source with time position
constrained to [startTime,stopTime]
. Media playback
commences at startTime
and continues to stopTime
.
The interval defined by these two endpoints is termed a cycle with
duration being the difference of the stop and start times. This cycle
may be set to repeat a specific or indefinite number of times. The total
duration of media playback is then the product of the cycle duration and the
number of times the cycle is played. If the stop time of the cycle is reached
and the cycle is to be played again, the event handler registered with the
onRepeat
property is invoked. If the stop time is reached and
the cycle is not to be repeated, then the event handler registered
with the onEndOfMedia
property is invoked. A zero-relative index of
which cycle is presently being played is maintained by currentCount
.
The operation of a MediaPlayer
is inherently asynchronous.
A player is not prepared to respond to commands quasi-immediately until
its status has transitioned to MediaPlayer.Status.READY
, which in
effect generally occurs when media pre-roll completes. Some requests made of
a player prior to its status being READY
will however take
effect when that status is entered. These include invoking play()
without an intervening invocation of pause()
or stop()
before the READY
transition, as well as setting any of the
autoPlay
, balance
,
mute
, rate
,
startTime
, stopTime
, and
volume
properties.
The status
property may be monitored to make the application aware of player status
changes, and callback functions may be registered via properties such as
onReady
if an action should be taken when a particular status is
entered. There are also error
and onError
properties which
respectively enable monitoring when an error occurs and taking a specified
action in response thereto.
The same MediaPlayer
object may be shared among multiple
MediaView
s. This will not affect the player itself. In
particular, the property settings of the view will not have any effect on
media playback.
Type | Property and Description |
---|---|
DoubleProperty |
audioSpectrumInterval
The interval between spectrum updates in seconds.
|
<any> |
audioSpectrumListener
A listener for audio spectrum updates.
|
IntegerProperty |
audioSpectrumNumBands
The number of bands in the audio spectrum.
|
IntegerProperty |
audioSpectrumThreshold
The sensitivity threshold in decibels; must be non-positive.
|
BooleanProperty |
autoPlay
Whether playing should start as soon as possible.
|
DoubleProperty |
balance
The balance, or left-right setting, of the audio output.
|
<any> |
bufferProgressTime
The current buffer position indicating how much media can be played
without stalling the
MediaPlayer . |
ReadOnlyIntegerProperty |
currentCount
The number of completed playback cycles.
|
ReadOnlyDoubleProperty |
currentRate
The current rate of playback regardless of settings.
|
<any> |
currentTime
The current media playback time.
|
IntegerProperty |
cycleCount
The number of times the media will be played.
|
<any> |
cycleDuration
|
<any> |
error
Observable property set to a
MediaException if an error occurs. |
BooleanProperty |
mute
Whether the player audio is muted.
|
<any> |
onEndOfMedia
Event handler invoked when the player
currentTime reaches
stopTime . |
<any> |
onError
Event handler invoked when an error occurs.
|
<any> |
onHalted
Event handler invoked when the status changes to
HALTED . |
<any> |
onMarker
Event handler invoked when the player
currentTime reaches a
media marker. |
<any> |
onPaused
Event handler invoked when the status changes to
PAUSED . |
<any> |
onPlaying
Event handler invoked when the status changes to
PLAYING . |
<any> |
onReady
Event handler invoked when the status changes to
READY . |
<any> |
onRepeat
Event handler invoked when the player
currentTime reaches
stopTime and will be repeating. |
<any> |
onStalled
Event handler invoked when the status changes to
STALLED . |
<any> |
onStopped
Event handler invoked when the status changes to
STOPPED . |
DoubleProperty |
rate
The rate at which the media should be played.
|
<any> |
startTime
The time offset where media should start playing, or restart from when
repeating.
|
<any> |
status
The current state of the MediaPlayer.
|
<any> |
stopTime
The time offset where media should stop playing or restart when repeating.
|
<any> |
totalDuration
The total amount of play time if allowed to play until finished.
|
DoubleProperty |
volume
The volume at which the media should be played.
|
Modifier and Type | Class and Description |
---|---|
private class |
MediaPlayer._BufferListener |
private class |
MediaPlayer._MarkerListener
Listener of marker events emitted by the implementation layer.
|
private class |
MediaPlayer._MediaErrorListener |
private class |
MediaPlayer._PlayerStateListener |
private class |
MediaPlayer._PlayerTimeListener |
private class |
MediaPlayer._SpectrumListener |
private class |
MediaPlayer._VideoTrackSizeListener |
private class |
MediaPlayer.InitMediaPlayer |
private class |
MediaPlayer.MarkerMapChangeListener
Listeners section
Listener of modifications to the marker map in the public Media API.
|
private class |
MediaPlayer.RendererListener |
static class |
MediaPlayer.Status
Enumeration describing the different status values of a
MediaPlayer . |
Modifier and Type | Field and Description |
---|---|
private AudioEqualizer |
audioEqualizer
The read-only
AudioEqualizer associated with this player. |
private static double |
AUDIOSPECTRUM_INTERVAL_MIN |
private static int |
AUDIOSPECTRUM_NUMBANDS_MIN |
private static int |
AUDIOSPECTRUM_THRESHOLD_MAX |
private boolean |
audioSpectrumEnabledChangeRequested |
private DoubleProperty |
audioSpectrumInterval
The interval between spectrum updates in seconds.
|
private boolean |
audioSpectrumIntervalChangeRequested |
private <any> |
audioSpectrumListener
A listener for audio spectrum updates.
|
private IntegerProperty |
audioSpectrumNumBands
The number of bands in the audio spectrum.
|
private boolean |
audioSpectrumNumBandsChangeRequested |
private IntegerProperty |
audioSpectrumThreshold
The sensitivity threshold in decibels; must be non-positive.
|
private boolean |
audioSpectrumThresholdChangeRequested |
private BooleanProperty |
autoPlay
Whether playing should start as soon as possible.
|
private DoubleProperty |
balance
The balance, or left-right setting, of the audio output.
|
private boolean |
balanceChangeRequested |
private BufferListener |
bufferListener |
private <any> |
bufferProgressTime
The current buffer position indicating how much media can be played
without stalling the
MediaPlayer . |
private ReadOnlyIntegerWrapper |
currentCount
The number of completed playback cycles.
|
private ReadOnlyDoubleWrapper |
currentRate
The current rate of playback regardless of settings.
|
private VideoDataBuffer |
currentRenderFrame |
private <any> |
currentTime
The current media playback time.
|
private IntegerProperty |
cycleCount
The number of times the media will be played.
|
private <any> |
cycleDuration
|
private static int |
DEFAULT_SPECTRUM_BAND_COUNT |
private static double |
DEFAULT_SPECTRUM_INTERVAL |
private static int |
DEFAULT_SPECTRUM_THRESHOLD |
private java.lang.Object |
disposeLock |
private <any> |
error
Observable property set to a
MediaException if an error occurs. |
private com.sun.media.jfxmedia.events.MediaErrorListener |
errorListener |
static int |
INDEFINITE
A value representing an effectively infinite number of playback cycles.
|
private boolean |
isEOS |
private boolean |
isUpdateTimeEnabled |
private com.sun.media.jfxmedia.MediaPlayer |
jfxPlayer |
private BufferProgressEvent |
lastBufferEvent |
private MarkerListener |
markerEventListener |
private <any> |
markerMapListener |
private Media |
media
The parent
Media object; read-only. |
private MediaTimerTask |
mediaTimerTask |
private BooleanProperty |
mute
Whether the player audio is muted.
|
private boolean |
muteChangeRequested |
private VideoDataBuffer |
nextRenderFrame |
private <any> |
onEndOfMedia
Event handler invoked when the player
currentTime reaches
stopTime . |
private <any> |
onError
Event handler invoked when an error occurs.
|
private <any> |
onHalted
Event handler invoked when the status changes to
HALTED . |
private <any> |
onMarker
Event handler invoked when the player
currentTime reaches a
media marker. |
private <any> |
onPaused
Event handler invoked when the status changes to
PAUSED . |
private <any> |
onPlaying
Event handler invoked when the status changes to
PLAYING . |
private <any> |
onReady
Event handler invoked when the status changes to
READY . |
private <any> |
onRepeat
Event handler invoked when the player
currentTime reaches
stopTime and will be repeating. |
private <any> |
onStalled
Event handler invoked when the status changes to
STALLED . |
private <any> |
onStopped
Event handler invoked when the status changes to
STOPPED . |
private boolean |
playerReady |
private boolean |
playRequested |
private double |
prevTimeMs |
private DoubleProperty |
rate
The rate at which the media should be played.
|
private static double |
RATE_MAX |
private static double |
RATE_MIN |
private boolean |
rateChangeRequested |
private MediaPlayer.RendererListener |
rendererListener |
private java.lang.Object |
renderLock |
private VideoTrackSizeListener |
sizeListener |
private com.sun.media.jfxmedia.events.AudioSpectrumListener |
spectrumListener |
private <any> |
startTime
The time offset where media should start playing, or restart from when
repeating.
|
private Duration |
startTimeAtStop |
private boolean |
startTimeChangeRequested |
private PlayerStateListener |
stateListener |
private <any> |
status
The current state of the MediaPlayer.
|
private <any> |
stopTime
The time offset where media should stop playing or restart when repeating.
|
private boolean |
stopTimeChangeRequested |
private PlayerTimeListener |
timeListener |
private <any> |
totalDuration
The total amount of play time if allowed to play until finished.
|
private java.util.Set<java.lang.ref.WeakReference<MediaView>> |
viewRefs |
private DoubleProperty |
volume
The volume at which the media should be played.
|
private boolean |
volumeChangeRequested |
Constructor and Description |
---|
MediaPlayer(Media media)
Create a player for a specific media.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
addView(MediaView view) |
DoubleProperty |
audioSpectrumIntervalProperty()
The interval between spectrum updates in seconds.
|
<any> |
audioSpectrumListenerProperty()
A listener for audio spectrum updates.
|
IntegerProperty |
audioSpectrumNumBandsProperty()
The number of bands in the audio spectrum.
|
IntegerProperty |
audioSpectrumThresholdProperty()
The sensitivity threshold in decibels; must be non-positive.
|
BooleanProperty |
autoPlayProperty()
Whether playing should start as soon as possible.
|
DoubleProperty |
balanceProperty()
The balance, or left-right setting, of the audio output.
|
<any> |
bufferProgressTimeProperty()
The current buffer position indicating how much media can be played
without stalling the
MediaPlayer . |
private <any> |
bufferProgressTimePropertyImpl() |
private void |
calculateCycleDuration() |
private double[] |
calculateStartStopTimes(Duration startValue,
Duration stopValue)
Behaviorally clamp the start and stop times.
|
private void |
calculateTotalDuration() |
private static double |
clamp(double dvalue,
double dmin,
double dmax) |
private static int |
clamp(int ivalue,
int imin,
int imax) |
(package private) void |
createMediaTimer() |
ReadOnlyIntegerProperty |
currentCountProperty()
The number of completed playback cycles.
|
private ReadOnlyIntegerWrapper |
currentCountPropertyImpl() |
ReadOnlyDoubleProperty |
currentRateProperty()
The current rate of playback regardless of settings.
|
private ReadOnlyDoubleWrapper |
currentRatePropertyImpl() |
<any> |
currentTimeProperty()
The current media playback time.
|
private <any> |
currentTimePropertyImpl() |
IntegerProperty |
cycleCountProperty()
The number of times the media will be played.
|
<any> |
cycleDurationProperty()
|
private <any> |
cycleDurationPropertyImpl() |
(package private) void |
destroyMediaTimer() |
void |
dispose()
Free all resources associated with player.
|
<any> |
errorProperty()
Observable property set to a
MediaException if an error occurs. |
private <any> |
errorPropertyImpl() |
AudioEqualizer |
getAudioEqualizer()
Retrieve the
AudioEqualizer associated with this player. |
double |
getAudioSpectrumInterval()
Retrieves the value of the audio spectrum notification interval in seconds.
|
AudioSpectrumListener |
getAudioSpectrumListener()
Retrieves the listener of the audio spectrum.
|
int |
getAudioSpectrumNumBands()
Retrieves the number of bands in the audio spectrum.
|
int |
getAudioSpectrumThreshold()
Retrieves the audio spectrum threshold in decibels.
|
double |
getBalance()
Retrieves the audio balance.
|
Duration |
getBufferProgressTime()
Retrieves the
bufferProgressTime value. |
int |
getCurrentCount()
Retrieves the index of the current cycle.
|
double |
getCurrentRate()
Retrieves the current playback rate.
|
Duration |
getCurrentTime()
Retrieves the current media time.
|
int |
getCycleCount()
Retrieves the cycle count.
|
Duration |
getCycleDuration()
Retrieves the cycle duration in seconds.
|
MediaException |
getError()
Retrieve the value of the
error property or null
if there is no error. |
(package private) VideoDataBuffer |
getLatestFrame()
WARNING: You must call releaseFrame() on the returned frame when you are
finished with it or a massive memory leak will occur.
|
Media |
getMedia()
Retrieves the
Media instance being played. |
java.lang.Runnable |
getOnEndOfMedia()
Retrieves the end of media event handler.
|
java.lang.Runnable |
getOnError()
Retrieves the event handler for errors.
|
java.lang.Runnable |
getOnHalted()
Retrieves the
MediaPlayer.Status.HALTED event handler. |
<any> |
getOnMarker()
Retrieves the marker event handler.
|
java.lang.Runnable |
getOnPaused()
Retrieves the
MediaPlayer.Status.PAUSED event handler. |
java.lang.Runnable |
getOnPlaying()
Retrieves the
MediaPlayer.Status.PLAYING event handler. |
java.lang.Runnable |
getOnReady()
Retrieves the
MediaPlayer.Status.READY event handler. |
java.lang.Runnable |
getOnRepeat()
Retrieves the repeat event handler.
|
java.lang.Runnable |
getOnStalled()
Retrieves the
MediaPlayer.Status.STALLED event handler. |
java.lang.Runnable |
getOnStopped()
Retrieves the
MediaPlayer.Status.STOPPED event handler. |
double |
getRate()
Retrieves the playback rate.
|
Duration |
getStartTime()
Retrieves the start time.
|
MediaPlayer.Status |
getStatus()
Retrieves the current player status.
|
Duration |
getStopTime()
Retrieves the stop time.
|
Duration |
getTotalDuration()
Retrieves the total playback duration including all cycles (repetitions).
|
double |
getVolume()
Retrieves the audio playback volume.
|
(package private) void |
handleError(MediaException error) |
(package private) void |
handleRequestedChanges() |
private void |
init() |
boolean |
isAutoPlay()
Retrieves the
autoPlay property value. |
boolean |
isMute()
Retrieves the
muteProperty() value. |
(package private) void |
loopPlayback() |
BooleanProperty |
muteProperty()
Whether the player audio is muted.
|
<any> |
onEndOfMediaProperty()
Event handler invoked when the player
currentTime reaches
stopTime . |
<any> |
onErrorProperty()
Event handler invoked when an error occurs.
|
<any> |
onHaltedProperty()
Event handler invoked when the status changes to
HALTED . |
<any> |
onMarkerProperty()
Event handler invoked when the player
currentTime reaches a
media marker. |
<any> |
onPausedProperty()
Event handler invoked when the status changes to
PAUSED . |
<any> |
onPlayingProperty()
Event handler invoked when the status changes to
PLAYING . |
<any> |
onReadyProperty()
Event handler invoked when the status changes to
READY . |
<any> |
onRepeatProperty()
Event handler invoked when the player
currentTime reaches
stopTime and will be repeating. |
<any> |
onStalledProperty()
Event handler invoked when the status changes to
STALLED . |
<any> |
onStoppedProperty()
Event handler invoked when the status changes to
STOPPED . |
void |
pause()
Pauses the player.
|
void |
play()
Starts playing the media.
|
(package private) void |
preReady() |
DoubleProperty |
rateProperty()
The rate at which the media should be played.
|
(package private) void |
registerListeners() |
(package private) void |
removeView(MediaView view) |
(package private) com.sun.media.jfxmedia.MediaPlayer |
retrieveJfxPlayer() |
void |
seek(Duration seekTime)
Seeks the player to a new playback time.
|
void |
setAudioSpectrumInterval(double value)
Sets the value of the audio spectrum notification interval in seconds.
|
void |
setAudioSpectrumListener(AudioSpectrumListener listener)
Sets the listener of the audio spectrum.
|
void |
setAudioSpectrumNumBands(int value)
Sets the number of bands in the audio spectrum.
|
void |
setAudioSpectrumThreshold(int value)
Sets the audio spectrum threshold in decibels.
|
void |
setAutoPlay(boolean value)
Sets the
autoPlay property value. |
void |
setBalance(double value)
Sets the audio balance.
|
private void |
setBufferProgressTime(Duration value) |
private void |
setCurrentCount(int value) |
private void |
setCurrentRate(double value) |
private void |
setCurrentTime(Duration value) |
void |
setCycleCount(int value)
Sets the cycle count.
|
private void |
setCycleDuration(Duration value) |
private void |
setError(MediaException value) |
void |
setMute(boolean value)
Sets the value of
muteProperty() . |
void |
setOnEndOfMedia(java.lang.Runnable value)
Sets the end of media event handler.
|
void |
setOnError(java.lang.Runnable value)
Sets the event handler to be called when an error occurs.
|
void |
setOnHalted(java.lang.Runnable value)
Sets the
MediaPlayer.Status.HALTED event handler. |
void |
setOnMarker(<any> onMarker)
Sets the marker event handler.
|
void |
setOnPaused(java.lang.Runnable value)
Sets the
MediaPlayer.Status.PAUSED event handler. |
void |
setOnPlaying(java.lang.Runnable value)
Sets the
MediaPlayer.Status.PLAYING event handler. |
void |
setOnReady(java.lang.Runnable value)
Sets the
MediaPlayer.Status.READY event handler. |
void |
setOnRepeat(java.lang.Runnable value)
Sets the repeat event handler.
|
void |
setOnStalled(java.lang.Runnable value)
Sets the
MediaPlayer.Status.STALLED event handler. |
void |
setOnStopped(java.lang.Runnable value)
Sets the
MediaPlayer.Status.STOPPED event handler. |
void |
setRate(double value)
Sets the playback rate to the supplied value.
|
private void |
setStartStopTimes(Duration startValue,
boolean isStartValueSet,
Duration stopValue,
boolean isStopValueSet)
Set the effective start and stop times on the underlying player,
clamping as needed.
|
void |
setStartTime(Duration value)
Sets the start time.
|
private void |
setStatus(MediaPlayer.Status value) |
void |
setStopTime(Duration value)
Sets the stop time.
|
private void |
setTotalDuration(Duration value) |
void |
setVolume(double value)
Sets the audio playback volume.
|
<any> |
startTimeProperty()
The time offset where media should start playing, or restart from when
repeating.
|
<any> |
statusProperty()
The current state of the MediaPlayer.
|
private <any> |
statusPropertyImpl() |
void |
stop()
Stops playing the media.
|
<any> |
stopTimeProperty()
The time offset where media should stop playing or restart when repeating.
|
<any> |
totalDurationProperty()
The total amount of play time if allowed to play until finished.
|
private <any> |
totalDurationPropertyImpl() |
(package private) void |
updateTime() |
DoubleProperty |
volumeProperty()
The volume at which the media should be played.
|
public <any> errorProperty
MediaException
if an error occurs.getError()
public <any> onErrorProperty
getOnError()
,
setOnError(Runnable)
public BooleanProperty autoPlayProperty
false
.isAutoPlay()
,
setAutoPlay(boolean)
public DoubleProperty rateProperty
1.0
plays the media at its normal (encoded) playback rate,
2.0
plays back at twice the normal rate, etc. The currently
supported range of rates is [0.0, 8.0]
. The default
value is 1.0
.getRate()
,
setRate(double)
public ReadOnlyDoubleProperty currentRateProperty
rate
is set to 1.0 and the player is paused or stalled,
then currentRate
will be zero.getCurrentRate()
public DoubleProperty volumeProperty
[0.0 1.0]
where 0.0
is inaudible
and 1.0
is full volume, which is the default.getVolume()
,
setVolume(double)
public DoubleProperty balanceProperty
[-1.0, 1.0]
with -1.0
being full left, 0.0
center, and 1.0
full right.
The default value is 0.0
.getBalance()
,
setBalance(double)
public <any> startTimeProperty
Duration.ZERO
.
Constraints: 0 ≤ startTime <
stopTime
getStartTime()
,
setStartTime(Duration)
public <any> stopTimeProperty
getMedia()
.getDuration()
.
Constraints: startTime
< stopTime ≤ Media.duration
getStopTime()
,
setStopTime(Duration)
public <any> cycleDurationProperty
startTime
and
stopTime
of this player. For the total duration of the Media use the
Media.duration
property.getCycleDuration()
public <any> totalDurationProperty
cycleCount
is set to INDEFINITE
then this will
also be INDEFINITE. If the Media duration is UNKNOWN, then this will
likewise be UNKNOWN. Otherwise, total duration will be the product of
cycleDuration and cycleCount.getTotalDuration()
public <any> currentTimeProperty
#seek(javafx.util.Duration)
to change playback to a different
stream position.getCurrentTime()
public <any> statusProperty
getStatus()
public <any> bufferProgressTimeProperty
MediaPlayer
. This is applicable to
buffered streams such as those reading from network connections as
opposed for example to local files.
Seeking to a position beyond bufferProgressTime
might
cause a slight pause in playback until an amount of data sufficient to
permit playback resumption has been buffered.
getBufferProgressTime()
public IntegerProperty cycleCountProperty
cycleCount
is set to 1
meaning the media will only be played once. Setting cycleCount
to a value greater than 1 will cause the media to play the given number
of times or until stopped. If set to INDEFINITE
,
playback will repeat until stop() or pause() is called.
constraints: cycleCount ≥ 1
getCycleCount()
,
setCycleCount(int)
public ReadOnlyIntegerProperty currentCountProperty
startTime
, i.e., when stopTime
or the
end of media has been reached.getCurrentCount()
public BooleanProperty muteProperty
true
indicates
that audio is not being produced. The value of this property has
no effect on volume
, i.e., if the audio is muted and then
un-muted, audio playback will resume at the same audible level provided
of course that the volume
property has not been modified
meanwhile. The default value is false
.isMute()
,
setMute(boolean)
public <any> onMarkerProperty
currentTime
reaches a
media marker.getOnMarker()
,
#setOnMarker()
public <any> onEndOfMediaProperty
currentTime
reaches
stopTime
.getOnEndOfMedia()
,
setOnEndOfMedia(Runnable)
public <any> onReadyProperty
READY
.getOnReady()
,
setOnReady(Runnable)
public <any> onPlayingProperty
PLAYING
.getOnPlaying()
,
setOnPlaying(Runnable)
public <any> onPausedProperty
PAUSED
.getOnPaused()
,
setOnPaused(Runnable)
public <any> onStoppedProperty
STOPPED
.getOnStopped()
,
setOnStopped(Runnable)
public <any> onHaltedProperty
HALTED
.getOnHalted()
,
setOnHalted(Runnable)
public <any> onRepeatProperty
currentTime
reaches
stopTime
and will be repeating. This callback is made
prior to seeking back to startTime
.getOnRepeat()
,
setOnRepeat(Runnable)
public <any> onStalledProperty
STALLED
.getOnStalled()
,
setOnStalled(Runnable)
public IntegerProperty audioSpectrumNumBandsProperty
[0.0, 22050]
Hz. If the
number of spectral bands were in this case set to 10, the width of each
frequency bin in the spectrum would be 2205
Hz with the
lower bound of the lowest frequency bin equal to 0.0
.public DoubleProperty audioSpectrumIntervalProperty
0.1
seconds.public IntegerProperty audioSpectrumThresholdProperty
public <any> audioSpectrumListenerProperty
An AudioSpectrumListener
may be useful for example to
plot the frequency spectrum of the audio being played or to generate
waveforms for a music visualizer.
public static final int INDEFINITE
cycleCount
is set to this value, the player
will replay the Media
until stopped or paused.private static final double RATE_MIN
private static final double RATE_MAX
private static final int AUDIOSPECTRUM_THRESHOLD_MAX
private static final double AUDIOSPECTRUM_INTERVAL_MIN
private static final int AUDIOSPECTRUM_NUMBANDS_MIN
private com.sun.media.jfxmedia.MediaPlayer jfxPlayer
private <any> markerMapListener
private MarkerListener markerEventListener
private PlayerStateListener stateListener
private PlayerTimeListener timeListener
private VideoTrackSizeListener sizeListener
private com.sun.media.jfxmedia.events.MediaErrorListener errorListener
private BufferListener bufferListener
private com.sun.media.jfxmedia.events.AudioSpectrumListener spectrumListener
private MediaPlayer.RendererListener rendererListener
private boolean rateChangeRequested
private boolean volumeChangeRequested
private boolean balanceChangeRequested
private boolean startTimeChangeRequested
private boolean stopTimeChangeRequested
private boolean muteChangeRequested
private boolean playRequested
private boolean audioSpectrumNumBandsChangeRequested
private boolean audioSpectrumIntervalChangeRequested
private boolean audioSpectrumThresholdChangeRequested
private boolean audioSpectrumEnabledChangeRequested
private MediaTimerTask mediaTimerTask
private double prevTimeMs
private boolean isUpdateTimeEnabled
private BufferProgressEvent lastBufferEvent
private Duration startTimeAtStop
private boolean isEOS
private final java.lang.Object disposeLock
private static final int DEFAULT_SPECTRUM_BAND_COUNT
private static final double DEFAULT_SPECTRUM_INTERVAL
private static final int DEFAULT_SPECTRUM_THRESHOLD
private final java.util.Set<java.lang.ref.WeakReference<MediaView>> viewRefs
private AudioEqualizer audioEqualizer
AudioEqualizer
associated with this player. The
equalizer is enabled by default.private <any> error
MediaException
if an error occurs.private <any> onError
private BooleanProperty autoPlay
false
.MediaPlayer.Status
private boolean playerReady
private DoubleProperty rate
1.0
plays the media at its normal (encoded) playback rate,
2.0
plays back at twice the normal rate, etc. The currently
supported range of rates is [0.0, 8.0]
. The default
value is 1.0
.private ReadOnlyDoubleWrapper currentRate
rate
is set to 1.0 and the player is paused or stalled,
then currentRate
will be zero.private DoubleProperty volume
[0.0 1.0]
where 0.0
is inaudible
and 1.0
is full volume, which is the default.private DoubleProperty balance
[-1.0, 1.0]
with -1.0
being full left, 0.0
center, and 1.0
full right.
The default value is 0.0
.private <any> startTime
Duration.ZERO
.
Constraints: 0 ≤ startTime <
stopTime
private <any> stopTime
getMedia()
.getDuration()
.
Constraints: startTime
< stopTime ≤ Media.duration
private <any> cycleDuration
startTime
and
stopTime
of this player. For the total duration of the Media use the
Media.duration
property.private <any> totalDuration
cycleCount
is set to INDEFINITE
then this will
also be INDEFINITE. If the Media duration is UNKNOWN, then this will
likewise be UNKNOWN. Otherwise, total duration will be the product of
cycleDuration and cycleCount.private <any> currentTime
#seek(javafx.util.Duration)
to change playback to a different
stream position.private <any> status
private <any> bufferProgressTime
MediaPlayer
. This is applicable to
buffered streams such as those reading from network connections as
opposed for example to local files.
Seeking to a position beyond bufferProgressTime
might
cause a slight pause in playback until an amount of data sufficient to
permit playback resumption has been buffered.
private IntegerProperty cycleCount
cycleCount
is set to 1
meaning the media will only be played once. Setting cycleCount
to a value greater than 1 will cause the media to play the given number
of times or until stopped. If set to INDEFINITE
,
playback will repeat until stop() or pause() is called.
constraints: cycleCount ≥ 1
private ReadOnlyIntegerWrapper currentCount
private BooleanProperty mute
true
indicates
that audio is not being produced. The value of this property has
no effect on volume
, i.e., if the audio is muted and then
un-muted, audio playback will resume at the same audible level provided
of course that the volume
property has not been modified
meanwhile. The default value is false
.volume
private <any> onMarker
currentTime
reaches a
media marker.private <any> onEndOfMedia
currentTime
reaches
stopTime
.private <any> onReady
READY
.private <any> onPlaying
PLAYING
.private <any> onPaused
PAUSED
.private <any> onStopped
STOPPED
.private <any> onHalted
HALTED
.private <any> onRepeat
currentTime
reaches
stopTime
and will be repeating. This callback is made
prior to seeking back to startTime
.cycleCount
private <any> onStalled
STALLED
.private IntegerProperty audioSpectrumNumBands
[0.0, 22050]
Hz. If the
number of spectral bands were in this case set to 10, the width of each
frequency bin in the spectrum would be 2205
Hz with the
lower bound of the lowest frequency bin equal to 0.0
.private DoubleProperty audioSpectrumInterval
0.1
seconds.private IntegerProperty audioSpectrumThreshold
private <any> audioSpectrumListener
An AudioSpectrumListener
may be useful for example to
plot the frequency spectrum of the audio being played or to generate
waveforms for a music visualizer.
private final java.lang.Object renderLock
private VideoDataBuffer currentRenderFrame
private VideoDataBuffer nextRenderFrame
public MediaPlayer(Media media)
Media
object with a MediaPlayer
: once the
player is created it cannot be changed. Errors which occur synchronously
within the constructor will cause exceptions to be thrown. Errors which
occur asynchronously will cause the error
property to be set and
consequently any onError
callback to be invoked.
When created, the status
of the player will be MediaPlayer.Status.UNKNOWN
.
Once the status
has transitioned to MediaPlayer.Status.READY
the
player will be in a usable condition. The amount of time between player
creation and its entering READY
status may vary depending,
for example, on whether the media is being read over a network connection
or from a local file system.
media
- The media to play.java.lang.NullPointerException
- if media is null
.MediaException
- if any synchronous errors occur within the
constructor.com.sun.media.jfxmedia.MediaPlayer retrieveJfxPlayer()
private static double clamp(double dvalue, double dmin, double dmax)
private static int clamp(int ivalue, int imin, int imax)
public final AudioEqualizer getAudioEqualizer()
AudioEqualizer
associated with this player.AudioEqualizer
or null
if player is disposed.void registerListeners()
private void init() throws MediaException
MediaException
private void setError(MediaException value)
public final MediaException getError()
error
property or null
if there is no error.MediaException
or null
.public <any> errorProperty()
MediaException
if an error occurs.getError()
private <any> errorPropertyImpl()
public final void setOnError(java.lang.Runnable value)
value
- the event handler or null
.public final java.lang.Runnable getOnError()
public <any> onErrorProperty()
getOnError()
,
setOnError(Runnable)
public final Media getMedia()
Media
instance being played.Media
object.public final void setAutoPlay(boolean value)
autoPlay
property value.value
- whether to enable auto-playbackpublic final boolean isAutoPlay()
autoPlay
property value.public BooleanProperty autoPlayProperty()
false
.isAutoPlay()
,
setAutoPlay(boolean)
public void play()
startTime
. When playing actually starts the
status
will be set to MediaPlayer.Status.PLAYING
.public void pause()
status
will be set to MediaPlayer.Status.PAUSED
.public void stop()
startTime
, and resets
currentCount
to zero. Once the player is actually
stopped, the status
will be set to MediaPlayer.Status.STOPPED
. The
only transitions out of STOPPED
status are to
MediaPlayer.Status.PAUSED
and MediaPlayer.Status.PLAYING
which occur after
invoking pause()
or play()
, respectively.
While stopped, the player will not respond to playback position changes
requested by #seek(javafx.util.Duration)
.public final void setRate(double value)
[0.0, 8.0]
.
Invoking this method will have no effect if media duration is Duration#INDEFINITE
.value
- the playback ratepublic final double getRate()
public DoubleProperty rateProperty()
1.0
plays the media at its normal (encoded) playback rate,
2.0
plays back at twice the normal rate, etc. The currently
supported range of rates is [0.0, 8.0]
. The default
value is 1.0
.getRate()
,
setRate(double)
private void setCurrentRate(double value)
public final double getCurrentRate()
public ReadOnlyDoubleProperty currentRateProperty()
rate
is set to 1.0 and the player is paused or stalled,
then currentRate
will be zero.getCurrentRate()
private ReadOnlyDoubleWrapper currentRatePropertyImpl()
public final void setVolume(double value)
[0.0, 1.0]
.value
- the volumepublic final double getVolume()
1.0
.public DoubleProperty volumeProperty()
[0.0 1.0]
where 0.0
is inaudible
and 1.0
is full volume, which is the default.getVolume()
,
setVolume(double)
public final void setBalance(double value)
[-1.0, 1.0]
.value
- the balancepublic final double getBalance()
public DoubleProperty balanceProperty()
[-1.0, 1.0]
with -1.0
being full left, 0.0
center, and 1.0
full right.
The default value is 0.0
.getBalance()
,
setBalance(double)
private double[] calculateStartStopTimes(Duration startValue, Duration stopValue)
[0.0, duration]
. If the duration is not
known, Double.MAX_VALUE
is used instead. Furthermore, if the
separately clamped values satisfy
startTime > stopTime
then stopTime
is clamped as
stopTime ≥ startTime
.startValue
- the new start time.stopValue
- the new stop time.{actualStart, actualStop}
.private void setStartStopTimes(Duration startValue, boolean isStartValueSet, Duration stopValue, boolean isStopValueSet)
startValue
- the new start time.stopValue
- the new stop time.public final void setStartTime(Duration value)
[Duration#ZERO
, stopTime
)
.
Invoking this method will have no effect if media duration is Duration#INDEFINITE
.value
- the start timepublic final Duration getStartTime()
Duration.ZERO
.public <any> startTimeProperty()
Duration.ZERO
.
Constraints: 0 ≤ startTime <
stopTime
getStartTime()
,
setStartTime(Duration)
public final void setStopTime(Duration value)
(startTime
, Media.duration
]
.
Invoking this method will have no effect if media duration is Duration#INDEFINITE
.value
- the stop timepublic final Duration getStopTime()
getMedia()
.getDuration()
. Note that
Media.duration
may have the value
Duration.UNKNOWN
if media initialization is not complete.public <any> stopTimeProperty()
getMedia()
.getDuration()
.
Constraints: startTime
< stopTime ≤ Media.duration
getStopTime()
,
setStopTime(Duration)
private void setCycleDuration(Duration value)
public final Duration getCycleDuration()
public <any> cycleDurationProperty()
startTime
and
stopTime
of this player. For the total duration of the Media use the
Media.duration
property.getCycleDuration()
private <any> cycleDurationPropertyImpl()
private void calculateCycleDuration()
private void setTotalDuration(Duration value)
public final Duration getTotalDuration()
public <any> totalDurationProperty()
cycleCount
is set to INDEFINITE
then this will
also be INDEFINITE. If the Media duration is UNKNOWN, then this will
likewise be UNKNOWN. Otherwise, total duration will be the product of
cycleDuration and cycleCount.getTotalDuration()
private <any> totalDurationPropertyImpl()
private void calculateTotalDuration()
private void setCurrentTime(Duration value)
public final Duration getCurrentTime()
public <any> currentTimeProperty()
#seek(javafx.util.Duration)
to change playback to a different
stream position.getCurrentTime()
private <any> currentTimePropertyImpl()
public void seek(Duration seekTime)
MediaPlayer.Status.STOPPED
or media duration is Duration#INDEFINITE
.
The behavior of seek()
is constrained as follows where
start time and stop time indicate the effective lower and
upper bounds, respectively, of media playback:
seekTime | seek position |
---|---|
null | no change |
Duration#UNKNOWN | no change |
Duration#INDEFINITE | stop time |
seekTime < start time | start time |
seekTime > stop time | stop time |
start time ≤ seekTime ≤ stop time | seekTime |
seekTime
- the requested playback timeprivate void setStatus(MediaPlayer.Status value)
public final MediaPlayer.Status getStatus()
public <any> statusProperty()
getStatus()
private <any> statusPropertyImpl()
private void setBufferProgressTime(Duration value)
public final Duration getBufferProgressTime()
bufferProgressTime
value.public <any> bufferProgressTimeProperty()
MediaPlayer
. This is applicable to
buffered streams such as those reading from network connections as
opposed for example to local files.
Seeking to a position beyond bufferProgressTime
might
cause a slight pause in playback until an amount of data sufficient to
permit playback resumption has been buffered.
getBufferProgressTime()
private <any> bufferProgressTimePropertyImpl()
public final void setCycleCount(int value)
[1,Integer.MAX_VALUE
]
.
Invoking this method will have no effect if media duration is Duration#INDEFINITE
.value
- the cycle countpublic final int getCycleCount()
public IntegerProperty cycleCountProperty()
cycleCount
is set to 1
meaning the media will only be played once. Setting cycleCount
to a value greater than 1 will cause the media to play the given number
of times or until stopped. If set to INDEFINITE
,
playback will repeat until stop() or pause() is called.
constraints: cycleCount ≥ 1
getCycleCount()
,
setCycleCount(int)
private void setCurrentCount(int value)
public final int getCurrentCount()
public ReadOnlyIntegerProperty currentCountProperty()
startTime
, i.e., when stopTime
or the
end of media has been reached.getCurrentCount()
private ReadOnlyIntegerWrapper currentCountPropertyImpl()
public final void setMute(boolean value)
muteProperty()
.value
- the mute
settingpublic final boolean isMute()
muteProperty()
value.public BooleanProperty muteProperty()
true
indicates
that audio is not being produced. The value of this property has
no effect on volume
, i.e., if the audio is muted and then
un-muted, audio playback will resume at the same audible level provided
of course that the volume
property has not been modified
meanwhile. The default value is false
.isMute()
,
setMute(boolean)
public final void setOnMarker(<any> onMarker)
onMarker
- the marker event handler.public final <any> getOnMarker()
public <any> onMarkerProperty()
currentTime
reaches a
media marker.getOnMarker()
,
#setOnMarker()
void addView(MediaView view)
void removeView(MediaView view)
void handleError(MediaException error)
void createMediaTimer()
void destroyMediaTimer()
void updateTime()
void loopPlayback()
void handleRequestedChanges()
void preReady()
public final void setOnEndOfMedia(java.lang.Runnable value)
value
- the event handler or null
.public final java.lang.Runnable getOnEndOfMedia()
null
.public <any> onEndOfMediaProperty()
currentTime
reaches
stopTime
.getOnEndOfMedia()
,
setOnEndOfMedia(Runnable)
public final void setOnReady(java.lang.Runnable value)
MediaPlayer.Status.READY
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnReady()
MediaPlayer.Status.READY
event handler.null
.public <any> onReadyProperty()
READY
.getOnReady()
,
setOnReady(Runnable)
public final void setOnPlaying(java.lang.Runnable value)
MediaPlayer.Status.PLAYING
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnPlaying()
MediaPlayer.Status.PLAYING
event handler.null
.public <any> onPlayingProperty()
PLAYING
.getOnPlaying()
,
setOnPlaying(Runnable)
public final void setOnPaused(java.lang.Runnable value)
MediaPlayer.Status.PAUSED
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnPaused()
MediaPlayer.Status.PAUSED
event handler.null
.public <any> onPausedProperty()
PAUSED
.getOnPaused()
,
setOnPaused(Runnable)
public final void setOnStopped(java.lang.Runnable value)
MediaPlayer.Status.STOPPED
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnStopped()
MediaPlayer.Status.STOPPED
event handler.null
.public <any> onStoppedProperty()
STOPPED
.getOnStopped()
,
setOnStopped(Runnable)
public final void setOnHalted(java.lang.Runnable value)
MediaPlayer.Status.HALTED
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnHalted()
MediaPlayer.Status.HALTED
event handler.null
.public <any> onHaltedProperty()
HALTED
.getOnHalted()
,
setOnHalted(Runnable)
public final void setOnRepeat(java.lang.Runnable value)
value
- the event handler or null
.public final java.lang.Runnable getOnRepeat()
null
.public <any> onRepeatProperty()
currentTime
reaches
stopTime
and will be repeating. This callback is made
prior to seeking back to startTime
.getOnRepeat()
,
setOnRepeat(Runnable)
public final void setOnStalled(java.lang.Runnable value)
MediaPlayer.Status.STALLED
event handler.value
- the event handler or null
.public final java.lang.Runnable getOnStalled()
MediaPlayer.Status.STALLED
event handler.null
.public <any> onStalledProperty()
STALLED
.getOnStalled()
,
setOnStalled(Runnable)
public final void setAudioSpectrumNumBands(int value)
value
- the number of spectral bands; value
must be ≥ 2public final int getAudioSpectrumNumBands()
public IntegerProperty audioSpectrumNumBandsProperty()
[0.0, 22050]
Hz. If the
number of spectral bands were in this case set to 10, the width of each
frequency bin in the spectrum would be 2205
Hz with the
lower bound of the lowest frequency bin equal to 0.0
.public final void setAudioSpectrumInterval(double value)
value
- a positive value specifying the spectral update intervalpublic final double getAudioSpectrumInterval()
public DoubleProperty audioSpectrumIntervalProperty()
0.1
seconds.public final void setAudioSpectrumThreshold(int value)
value
- the spectral threshold in dB; must be ≤ 0
.public final int getAudioSpectrumThreshold()
public IntegerProperty audioSpectrumThresholdProperty()
public final void setAudioSpectrumListener(AudioSpectrumListener listener)
listener
- the spectral listener or null
.public final AudioSpectrumListener getAudioSpectrumListener()
null
public <any> audioSpectrumListenerProperty()
An AudioSpectrumListener
may be useful for example to
plot the frequency spectrum of the audio being played or to generate
waveforms for a music visualizer.
public void dispose()
MediaPlayer.Status.DISPOSED
after this method is done. This method can be
called anytime regardless of current player status.VideoDataBuffer getLatestFrame()