public class AudioPlayer
extends java.lang.Object
To help with quick conversions from the Android MediaPlayer this class follows the APIs the Android MediaPlayer provides.
| Modifier and Type | Field and Description |
|---|---|
protected AudioPlayerApi |
audioPlayerImpl |
protected ListenerMux |
listenerMux |
protected long |
overriddenDuration |
| Constructor and Description |
|---|
AudioPlayer(AudioPlayerApi audioPlayerImpl) |
AudioPlayer(android.content.Context context) |
AudioPlayer(android.content.Context context,
DeviceUtil deviceUtil) |
| Modifier and Type | Method and Description |
|---|---|
int |
getAudioSessionId()
Returns the audio session ID.
|
java.util.Map<ExoMedia.RendererType,com.google.android.exoplayer2.source.TrackGroupArray> |
getAvailableTracks()
Retrieves a list of available tracks to select from.
|
int |
getBufferPercentage()
Retrieves the current buffer percent of the audio item.
|
long |
getCurrentPosition()
Retrieves the current position of the audio playback.
|
long |
getDuration()
Retrieves the duration of the current audio item.
|
protected void |
init(AudioPlayerApi audioPlayerImpl) |
boolean |
isPlaying()
Returns if an audio item is currently in playback
|
void |
overrideDuration(long duration)
Setting this will override the duration that the item may actually be.
|
void |
pause()
If an audio item is currently in playback, it will be paused
|
void |
prepareAsync()
Prepares the media specified with
setDataSource(Uri) or
setDataSource(Uri, MediaSource) in an asynchronous manner |
void |
release()
Releases the resources associated with this media player
|
void |
reset()
Stops the current audio playback and resets the listener states
so that we receive the callbacks for events like onPrepared
|
void |
seekTo(long milliSeconds)
Moves the current audio progress to the specified location.
|
void |
setAudioStreamType(int streamType)
Sets the audio stream type for this MediaPlayer.
|
void |
setDataSource(android.net.Uri uri)
Sets the source path for the audio item.
|
void |
setDataSource(android.net.Uri uri,
com.google.android.exoplayer2.source.MediaSource mediaSource)
Sets the source path for the audio item.
|
void |
setDrmCallback(com.google.android.exoplayer2.drm.MediaDrmCallback drmCallback)
Sets the
MediaDrmCallback to use when handling DRM for media. |
void |
setMetadataListener(MetadataListener listener)
Sets the listener to inform of ID3 metadata updates
|
void |
setOnBufferUpdateListener(OnBufferUpdateListener listener)
Sets the listener to inform of VideoPlayer buffer update events
|
void |
setOnCompletionListener(OnCompletionListener listener)
Sets the listener to inform of VideoPlayer completion events
|
void |
setOnErrorListener(OnErrorListener listener)
Sets the listener to inform of playback errors
|
void |
setOnPreparedListener(OnPreparedListener listener)
Sets the listener to inform of VideoPlayer prepared events
|
void |
setOnSeekCompletionListener(OnSeekCompletionListener listener)
Sets the listener to inform of VideoPlayer seek completion events
|
boolean |
setPlaybackSpeed(float speed)
Sets the playback speed for this MediaPlayer.
|
void |
setTrack(ExoMedia.RendererType trackType,
int trackIndex)
Changes to the track with
trackIndex for the specified
trackType |
void |
setVolume(float leftVolume,
float rightVolume)
Sets the volume level for the audio playback.
|
void |
setWakeMode(android.content.Context context,
int mode)
Set the low-level power management behavior for this AudioPlayer.
|
void |
start()
Starts the playback for the audio item specified in
setDataSource(Uri). |
void |
stopPlayback()
If an audio item is currently in playback then the playback will be stopped
|
boolean |
trackSelectionAvailable()
Determines if the current video player implementation supports
track selection for audio or video tracks.
|
protected ListenerMux listenerMux
protected AudioPlayerApi audioPlayerImpl
protected long overriddenDuration
public AudioPlayer(@NonNull
android.content.Context context)
public AudioPlayer(@NonNull
android.content.Context context,
@NonNull
DeviceUtil deviceUtil)
public AudioPlayer(AudioPlayerApi audioPlayerImpl)
protected void init(AudioPlayerApi audioPlayerImpl)
public int getAudioSessionId()
public boolean setPlaybackSpeed(float speed)
speed - The speed to play the media back atpublic void setAudioStreamType(int streamType)
AudioManager
for a list of stream types. Must call this method before prepare() or
prepareAsync() in order for the target stream type to become effective
thereafter.streamType - The audio stream typeAudioManagerpublic void setDataSource(@Nullable
android.net.Uri uri)
uri - The Uri representing the path to the audio itempublic void setDataSource(@Nullable
android.net.Uri uri,
@Nullable
com.google.android.exoplayer2.source.MediaSource mediaSource)
uri - The Uri representing the path to the audio itemmediaSource - The MediaSource to use for audio playbackpublic void setDrmCallback(@Nullable
com.google.android.exoplayer2.drm.MediaDrmCallback drmCallback)
MediaDrmCallback to use when handling DRM for media.
This should be called before specifying the videos uri or path
drmCallback - The callback to use when handling DRM mediapublic void prepareAsync()
setDataSource(Uri) or
setDataSource(Uri, MediaSource) in an asynchronous mannerpublic void setVolume(float leftVolume,
float rightVolume)
leftVolume - The volume range [0.0 - 1.0]rightVolume - The volume range [0.0 - 1.0]public void setWakeMode(android.content.Context context,
int mode)
This function has the AudioPlayer access the low-level power manager
service to control the device's power usage while playing is occurring.
The parameter is a combination of PowerManager wake flags.
Use of this method requires Manifest.permission.WAKE_LOCK
permission.
By default, no attempt is made to keep the device awake during playback.
context - the Context to usemode - the power/wake mode to setPowerManagerpublic void reset()
public void seekTo(long milliSeconds)
setOnPreparedListener(OnPreparedListener)milliSeconds - The time to move the playback topublic boolean isPlaying()
public void start()
setDataSource(Uri).
This should be called after the AudioPlayer is correctly prepared (see setOnPreparedListener(OnPreparedListener))public void pause()
public void stopPlayback()
public void release()
public long getDuration()
setOnPreparedListener(OnPreparedListener)).
If overrideDuration(long) is set then that value will be returned.public void overrideDuration(long duration)
duration - The duration for the current media item or < 0 to disablepublic long getCurrentPosition()
setOnPreparedListener(OnPreparedListener))public int getBufferPercentage()
setOnPreparedListener(OnPreparedListener))public boolean trackSelectionAvailable()
public void setTrack(ExoMedia.RendererType trackType, int trackIndex)
trackIndex for the specified
trackTypetrackType - The type for the track to switch to the selected indextrackIndex - The index for the track to switch to@Nullable public java.util.Map<ExoMedia.RendererType,com.google.android.exoplayer2.source.TrackGroupArray> getAvailableTracks()
trackSelectionAvailable()
should be called before this.public void setOnPreparedListener(@Nullable
OnPreparedListener listener)
listener - The listenerpublic void setOnCompletionListener(@Nullable
OnCompletionListener listener)
listener - The listenerpublic void setOnBufferUpdateListener(@Nullable
OnBufferUpdateListener listener)
listener - The listenerpublic void setOnSeekCompletionListener(@Nullable
OnSeekCompletionListener listener)
listener - The listenerpublic void setOnErrorListener(@Nullable
OnErrorListener listener)
listener - The listenerpublic void setMetadataListener(@Nullable
MetadataListener listener)
listener - The listener to inform