Class DefaultAudioPlayer
- java.lang.Object
-
- com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayer
-
- All Implemented Interfaces:
AudioPlayer,AudioFrameProvider,TrackStateListener
public class DefaultAudioPlayer extends java.lang.Object implements AudioPlayer, TrackStateListener
An audio player that is capable of playing audio tracks and provides audio frames from the currently playing track.
-
-
Constructor Summary
Constructors Constructor Description DefaultAudioPlayer(DefaultAudioPlayerManager manager)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(AudioEventListener listener)Add a listener to events from this player.voidcheckCleanup(long threshold)Check if the player should be "cleaned up" - stopped due to nothing using it, with the given threshold.voiddestroy()Destroy the player and stop playing track.AudioTrackgetPlayingTrack()intgetVolume()booleanisPaused()voidonTrackException(AudioTrack track, FriendlyException exception)Called when an exception occurs while a track is playing or loading.voidonTrackStuck(AudioTrack track, long thresholdMs)Called when an exception occurs while a track is playing or loading.voidplayTrack(AudioTrack track)AudioFrameprovide()AudioFrameprovide(long timeout, java.util.concurrent.TimeUnit unit)booleanprovide(MutableAudioFrame targetFrame)booleanprovide(MutableAudioFrame targetFrame, long timeout, java.util.concurrent.TimeUnit unit)voidremoveListener(AudioEventListener listener)Remove an attached listener using identity comparison.voidsetFilterFactory(PcmFilterFactory factory)voidsetFrameBufferDuration(java.lang.Integer duration)voidsetPaused(boolean value)voidsetVolume(int volume)booleanstartTrack(AudioTrack track, boolean noInterrupt)voidstopTrack()Stop currently playing track.
-
-
-
Constructor Detail
-
DefaultAudioPlayer
public DefaultAudioPlayer(DefaultAudioPlayerManager manager)
- Parameters:
manager- Audio player manager which this player is attached to
-
-
Method Detail
-
getPlayingTrack
public AudioTrack getPlayingTrack()
- Specified by:
getPlayingTrackin interfaceAudioPlayer- Returns:
- Currently playing track
-
playTrack
public void playTrack(AudioTrack track)
- Specified by:
playTrackin interfaceAudioPlayer- Parameters:
track- The track to start playing
-
startTrack
public boolean startTrack(AudioTrack track, boolean noInterrupt)
- Specified by:
startTrackin interfaceAudioPlayer- Parameters:
track- The track to start playing, passing null will stop the current track and return falsenoInterrupt- Whether to only start if nothing else is playing- Returns:
- True if the track was started
-
stopTrack
public void stopTrack()
Stop currently playing track.- Specified by:
stopTrackin interfaceAudioPlayer
-
provide
public AudioFrame provide()
- Specified by:
providein interfaceAudioFrameProvider- Returns:
- Provided frame, or null if none available
-
provide
public AudioFrame provide(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
- Specified by:
providein interfaceAudioFrameProvider- Parameters:
timeout- Specifies the maximum time to wait for data. Pass 0 for non-blocking mode.unit- Specifies the time unit of the maximum wait time.- Returns:
- Provided frame. In case wait time is above zero, null indicates that no data is not available at the current moment, otherwise null means the end of the track.
- Throws:
java.util.concurrent.TimeoutException- When wait time is above zero, but no track info is found in that time.java.lang.InterruptedException- When interrupted externally (or for seek/stop).
-
provide
public boolean provide(MutableAudioFrame targetFrame)
- Specified by:
providein interfaceAudioFrameProvider- Parameters:
targetFrame- Frame to update with the details and data of the provided frame.- Returns:
trueif a frame was provided.
-
provide
public boolean provide(MutableAudioFrame targetFrame, long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
- Specified by:
providein interfaceAudioFrameProvider- Parameters:
targetFrame- Frame to update with the details and data of the provided frame.timeout- Timeout.unit- Time unit for the timeout value.- Returns:
trueif a frame was provided.- Throws:
java.util.concurrent.TimeoutException- If no frame became available within the timeout.java.lang.InterruptedException- When interrupted externally (or for seek/stop).
-
getVolume
public int getVolume()
- Specified by:
getVolumein interfaceAudioPlayer
-
setVolume
public void setVolume(int volume)
- Specified by:
setVolumein interfaceAudioPlayer
-
setFilterFactory
public void setFilterFactory(PcmFilterFactory factory)
- Specified by:
setFilterFactoryin interfaceAudioPlayer
-
setFrameBufferDuration
public void setFrameBufferDuration(java.lang.Integer duration)
- Specified by:
setFrameBufferDurationin interfaceAudioPlayer
-
isPaused
public boolean isPaused()
- Specified by:
isPausedin interfaceAudioPlayer- Returns:
- Whether the player is paused
-
setPaused
public void setPaused(boolean value)
- Specified by:
setPausedin interfaceAudioPlayer- Parameters:
value- True to pause, false to resume
-
destroy
public void destroy()
Destroy the player and stop playing track.- Specified by:
destroyin interfaceAudioPlayer
-
addListener
public void addListener(AudioEventListener listener)
Add a listener to events from this player.- Specified by:
addListenerin interfaceAudioPlayer- Parameters:
listener- New listener
-
removeListener
public void removeListener(AudioEventListener listener)
Remove an attached listener using identity comparison.- Specified by:
removeListenerin interfaceAudioPlayer- Parameters:
listener- The listener to remove
-
onTrackException
public void onTrackException(AudioTrack track, FriendlyException exception)
Description copied from interface:TrackStateListenerCalled when an exception occurs while a track is playing or loading. This is always fatal, but it may have left some data in the audio buffer which can still play until the buffer clears out.- Specified by:
onTrackExceptionin interfaceTrackStateListener- Parameters:
track- The audio track for which the exception occurredexception- The exception that occurred
-
onTrackStuck
public void onTrackStuck(AudioTrack track, long thresholdMs)
Description copied from interface:TrackStateListenerCalled when an exception occurs while a track is playing or loading. This is always fatal, but it may have left some data in the audio buffer which can still play until the buffer clears out.- Specified by:
onTrackStuckin interfaceTrackStateListener- Parameters:
track- The audio track for which the exception occurredthresholdMs- The wait threshold that was exceeded for this event to trigger
-
checkCleanup
public void checkCleanup(long threshold)
Check if the player should be "cleaned up" - stopped due to nothing using it, with the given threshold.- Specified by:
checkCleanupin interfaceAudioPlayer- Parameters:
threshold- Threshold in milliseconds to use
-
-