Class AudioClip
- javafx.scene.media.AudioClip
public final class AudioClip extends Object
AudioClip represents a segment of audio that can be played with minimal latency. Clips are loaded similarly to Media objects but have different behavior, for example, a Media cannot play itself. AudioClips are also usable immediately. Playback behavior is fire and forget: once one of the play methods is called the only operable control is stop(). An AudioClip may also be played multiple times simultaneously. To accomplish the same task using Media one would have to create a new MediaPlayer object for each sound played in parallel. Media objects are however better suited for long-playing sounds. This is primarily because AudioClip stores in memory the raw, uncompressed audio data for the entire sound, which can be quite large for long audio clips. A MediaPlayer will only have enough decompressed audio data pre-rolled in memory to play for a short amount of time so it is much more memory efficient for long clips, especially if they are compressed. Example usage:
AudioClip plonkSound = new AudioClip("http://somehost/path/plonk.aiff");
plonkSound.play(); Since:
JavaFX 2.0
-
Property Summary
All MethodsInstance MethodsConcrete Methods Type Property and Description DoublePropertybalanceThe relative left and right volume levels of the clip.IntegerPropertycycleCountThe number of times the clip will be played whenplay()is called.DoublePropertypanThe relative "center" of the clip.IntegerPropertypriorityThe relative priority of the clip with respect to other clips.DoublePropertyrateThe relative rate at which the clip is played.DoublePropertyvolumeThe relative volume level at which the clip is played.
-
Field Summary
Fields Modifier and Type Field and Description static intINDEFINITEWhencycleCountis set to this value, theAudioClipwill loop continuously until stopped.
-
Constructor Summary
Constructors Constructor and Description AudioClip(String source)Create anAudioCliploaded from the supplied source URL.
-
Method Summary
All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description DoublePropertybalanceProperty()The relative left and right volume levels of the clip.IntegerPropertycycleCountProperty()The number of times the clip will be played whenplay()is called.doublegetBalance()Get the default balance level for this clip.intgetCycleCount()Get the default cycle count.doublegetPan()Get the default pan value.intgetPriority()Get the default playback priority.doublegetRate()Get the default playback rate.StringgetSource()Get the source URL used to create thisAudioClip.doublegetVolume()Get the default volume level.booleanisPlaying()Indicate whether thisAudioClipis playing.DoublePropertypanProperty()The relative "center" of the clip.voidplay()Play theAudioClipusing all the default parameters.voidplay(double volume)Play theAudioClipusing all the default parameters except volume.voidplay(double volume, double balance, double rate, double pan, int priority)Play theAudioClipusing the given parameters.IntegerPropertypriorityProperty()The relative priority of the clip with respect to other clips.DoublePropertyrateProperty()The relative rate at which the clip is played.voidsetBalance(double balance)Set the default balance level.voidsetCycleCount(int count)Set the default cycle count.voidsetPan(double pan)Set the default pan value.voidsetPriority(int priority)Set the default playback priority.voidsetRate(double rate)Set the default playback rate.voidsetVolume(double value)Set the default volume level.voidstop()Immediately stop all playback of thisAudioClip.DoublePropertyvolumeProperty()The relative volume level at which the clip is played.
-
Property Detail
-
volume
public DoubleProperty volumeProperty
The relative volume level at which the clip is played. Valid range is 0.0 (muted) to 1.0 (full volume). Values are clamped to this range internally so values outside this range will have no additional effect. Volume is controlled by attenuation, so values below 1.0 will reduce the sound level accordingly.See Also:
-
balance
public DoubleProperty balanceProperty
The relative left and right volume levels of the clip. Valid range is -1.0 to 1.0 where -1.0 gives full volume to the left channel while muting the right channel, 0.0 gives full volume to both channels and 1.0 gives full volume to right channel and mutes the left channel. Values outside this range are clamped internally.See Also:
-
rate
public DoubleProperty rateProperty
The relative rate at which the clip is played. Valid range is 0.125 (1/8 speed) to 8.0 (8x speed); values outside this range are clamped internally. Normal playback for a clip is 1.0; any other rate will affect pitch and duration accordingly.See Also:
-
pan
public DoubleProperty panProperty
The relative "center" of the clip. A pan value of 0.0 plays the clip normally where a -1.0 pan shifts the clip entirely to the left channel and 1.0 shifts entirely to the right channel. Unlike balance this setting mixes both channels so neither channel loses data. Setting pan on a mono clip has the same effect as setting balance, but with a much higher cost in CPU overhead so this is not recommended for mono clips.See Also:
-
priority
public IntegerProperty priorityProperty
The relative priority of the clip with respect to other clips. This value is used to determine which clips to remove when the maximum allowed number of clips is exceeded. The lower the priority, the more likely the clip is to be stopped and removed from the mixer channel it is occupying. Valid range is any integer; there are no constraints. The default priority is zero for all clips until changed. The number of simultaneous sounds that can be played is implementation- and possibly system-dependent.See Also:
-
cycleCount
public IntegerProperty cycleCountProperty
The number of times the clip will be played whenplay()is called. A cycleCount of 1 plays exactly once, a cycleCount of 2 plays twice and so on. Valid range is 1 or more, but setting this toINDEFINITEwill cause the clip to continue looping untilstop()is called.See Also:
-
-
Field Detail
-
INDEFINITE
public static final int INDEFINITE
WhencycleCountis set to this value, theAudioClipwill loop continuously until stopped. This value is synonymous withMediaPlayer.INDEFINITEandAnimation.INDEFINITE, these values may be used interchangeably.See Also:
-
-
Constructor Detail
-
AudioClip
public AudioClip(String source)
Create anAudioCliploaded from the supplied source URL.Parameters:
source- URL string from which to load the audio clip. This can be an HTTP, file or jar source.Throws:
NullPointerException- if the parameter isnull.
-
-
Method Detail
-
getSource
public String getSource()
Get the source URL used to create thisAudioClip.Returns:
source URL as provided to the constructor
-
setVolume
public final void setVolume(double value)
Set the default volume level. The new setting will only take effect on subsequent plays.Parameters:
value- new default volume level for this clipSee Also:
-
getVolume
public final double getVolume()
Get the default volume level.Returns:
the default volume level for this clip
See Also:
-
volumeProperty
public DoubleProperty volumeProperty()
The relative volume level at which the clip is played. Valid range is 0.0 (muted) to 1.0 (full volume). Values are clamped to this range internally so values outside this range will have no additional effect. Volume is controlled by attenuation, so values below 1.0 will reduce the sound level accordingly.See Also:
-
setBalance
public void setBalance(double balance)
Set the default balance level. The new value will only affect subsequent plays.Parameters:
balance- new default balanceSee Also:
-
getBalance
public double getBalance()
Get the default balance level for this clip.Returns:
the default balance for this clip
See Also:
-
balanceProperty
public DoubleProperty balanceProperty()
The relative left and right volume levels of the clip. Valid range is -1.0 to 1.0 where -1.0 gives full volume to the left channel while muting the right channel, 0.0 gives full volume to both channels and 1.0 gives full volume to right channel and mutes the left channel. Values outside this range are clamped internally.See Also:
-
setRate
public void setRate(double rate)
Set the default playback rate. The new value will only affect subsequent plays.Parameters:
rate- the new default playback rateSee Also:
-
getRate
public double getRate()
Get the default playback rate.Returns:
default playback rate for this clip
See Also:
-
rateProperty
public DoubleProperty rateProperty()
The relative rate at which the clip is played. Valid range is 0.125 (1/8 speed) to 8.0 (8x speed); values outside this range are clamped internally. Normal playback for a clip is 1.0; any other rate will affect pitch and duration accordingly.See Also:
-
setPan
public void setPan(double pan)
Set the default pan value. The new value will only affect subsequent plays.Parameters:
pan- the new default pan valueSee Also:
-
getPan
public double getPan()
Get the default pan value.Returns:
the default pan value for this clip
See Also:
-
panProperty
public DoubleProperty panProperty()
The relative "center" of the clip. A pan value of 0.0 plays the clip normally where a -1.0 pan shifts the clip entirely to the left channel and 1.0 shifts entirely to the right channel. Unlike balance this setting mixes both channels so neither channel loses data. Setting pan on a mono clip has the same effect as setting balance, but with a much higher cost in CPU overhead so this is not recommended for mono clips.See Also:
-
setPriority
public void setPriority(int priority)
Set the default playback priority. The new value will only affect subsequent plays.Parameters:
priority- the new default playback prioritySee Also:
-
getPriority
public int getPriority()
Get the default playback priority.Returns:
the default playback priority of this clip
See Also:
-
priorityProperty
public IntegerProperty priorityProperty()
The relative priority of the clip with respect to other clips. This value is used to determine which clips to remove when the maximum allowed number of clips is exceeded. The lower the priority, the more likely the clip is to be stopped and removed from the mixer channel it is occupying. Valid range is any integer; there are no constraints. The default priority is zero for all clips until changed. The number of simultaneous sounds that can be played is implementation- and possibly system-dependent.See Also:
-
setCycleCount
public void setCycleCount(int count)
Set the default cycle count. The new value will only affect subsequent plays.Parameters:
count- the new default cycle count for this clipSee Also:
-
getCycleCount
public int getCycleCount()
Get the default cycle count.Returns:
the default cycleCount for this audio clip
See Also:
-
cycleCountProperty
public IntegerProperty cycleCountProperty()
The number of times the clip will be played whenplay()is called. A cycleCount of 1 plays exactly once, a cycleCount of 2 plays twice and so on. Valid range is 1 or more, but setting this toINDEFINITEwill cause the clip to continue looping untilstop()is called.See Also:
-
play
public void play()
Play theAudioClipusing all the default parameters.
-
play
public void play(double volume)
Play theAudioClipusing all the default parameters except volume. This method does not modify the clip's default parameters.Parameters:
volume- the volume level at which to play the clip
-
play
public void play(double volume, double balance, double rate, double pan, int priority)Play theAudioClipusing the given parameters. Values outside the ranges as specified by their associated properties are clamped. This method does not modify the clip's default parameters.Parameters:
volume- Volume level at which to play this clip. Valid volume range is 0.0 to 1.0, where 0.0 is effectively muted and 1.0 is full volume.
-
isPlaying
public boolean isPlaying()
Indicate whether thisAudioClipis playing. If this returns true thenplay()has been called at least once and it is still playing.Returns:
true if any mixer channel has this clip queued, false otherwise
-
stop
public void stop()
Immediately stop all playback of thisAudioClip.
-
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.