Google 致力于为黑人社区推动种族平等。查看具体举措

MediaPlayer

class MediaPlayer : SessionPlayer
kotlin.Any
   ↳ androidx.media2.common.SessionPlayer
   ↳ androidx.media2.player.MediaPlayer

A media player which plays MediaItems. The details on playback control and player states can be found in the documentation of the base class, SessionPlayer.

Topic covered here:

  1. Audio focus and noisy intent

Audio focus and noisy intent

By default, MediaPlayer handles audio focus and noisy intent with AudioAttributesCompat set to this player. You need to call setAudioAttributes(AudioAttributesCompat) set the audio attribute while in the PLAYER_STATE_IDLE.

Here's the table of automatic audio focus behavior with audio attributes.

Audio Attributes Audio Focus Gain Type Misc
AudioAttributesCompat#USAGE_VOICE_COMMUNICATION_SIGNALLING android.media.AudioManager#AUDIOFOCUS_NONE
android.media.AudioManager#AUDIOFOCUS_GAIN Developers should specific a proper usage instead of AudioAttributesCompat#USAGE_UNKNOWN
android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT
android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE
AudioAttributesCompat#USAGE_ASSISTANCE_ACCESSIBILITY android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT if AudioAttributesCompat#CONTENT_TYPE_SPEECH, android.media.AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK otherwise
null No audio focus handling, and sets the player volume to 0 Only valid if your media contents don't have audio
Any other AudioAttributes No audio focus handling, and sets the player volume to 0 This is to handle error

If an AudioAttributesCompat is not specified by setAudioAttributes, getAudioAttributes will return null and the default audio focus behavior will follow the null case on the table above.

For more information about the audio focus, take a look at Managing audio focus

Summary

Nested classes
abstract

Interface definition for callbacks to be invoked when the player has the corresponding events.

Class for the player to return each audio/video/subtitle track's metadata.

Constants
static Int

Informs that audio is not playing.

static Int

Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e.

static Int

Update status in buffering a media source received through progressive downloading.

static Int

A new set of metadata is available.

static Int

The media cannot be seeked (e.g live stream)

static Int

Informs that video is not playing.

static Int

The player just pushed the very first video frame for rendering.

static Int

The video is too complex for the decoder: it can't decode frames fast enough.

static Int

The return value of getSelectedTrack(int) when there is no selected track for the given type.

static Int

File or network related operation errors.

static Int

Bitstream is not conforming to the related coding standard or file spec.

static Int

Some operation takes too long to complete, usually more than 3-5 seconds.

static Int

Unspecified player error.

static Int

Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature.

static Int

This mode is used with seekTo(long, int) to move media position to a frame (not necessarily a key frame) associated with a media item that is located closest to or at the given time.

static Int

This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a media item that is located closest to (in time) or at the given time.

static Int

This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a media item that is located right after or at the given time.

static Int

This mode is used with seekTo(long, int) to move media position to a sync (or key) frame associated with a media item that is located right before or at the given time.

Inherited constants
Public constructors
<init>(@NonNull context: Context)

Constructor to create a MediaPlayer instance.

Public methods
ListenableFuture<SessionPlayer.PlayerResult!>
addPlaylistItem(index: Int, @NonNull item: MediaItem)

Adds the media item to the playlist at the index.

ListenableFuture<SessionPlayer.PlayerResult!>
attachAuxEffect(effectId: Int)

Attaches an auxiliary effect to the player.

Unit

Closes the player and relinquish underlying resources.

ListenableFuture<SessionPlayer.PlayerResult!>

Deselects the TrackInfo for the current media item.

AudioAttributesCompat?

Int

Returns the audio session ID.

Long

Int

MediaItem?

Int

Gets the index of current media item in playlist.

Long

Long

Float

Int

Gets the next item index in the playlist.

PlaybackParams

Gets the playback params, containing the current playback rate.

Float

Int

Float

MutableList<MediaItem!>?

MediaMetadata?

Int

Gets the previous item index in the playlist.

Int

MediaPlayer.TrackInfo?
getSelectedTrack(trackType: Int)

Returns the selected track for the given track type.

Int

MediaTimestamp?

Gets current playback position as a MediaTimestamp.

MutableList<MediaPlayer.TrackInfo!>

MutableList<SessionPlayer.TrackInfo!>

Gets the full list of selected and unselected tracks that the media contains.

VideoSize

Returns the size of the video.

ListenableFuture<SessionPlayer.PlayerResult!>
movePlaylistItem(fromIndex: Int, toIndex: Int)

Moves the media item at fromIdx to toIdx in the playlist.

ListenableFuture<SessionPlayer.PlayerResult!>

Pauses playback.

ListenableFuture<SessionPlayer.PlayerResult!>

Starts or resumes playback.

ListenableFuture<SessionPlayer.PlayerResult!>

Prepares the media items for playback.

Unit
registerPlayerCallback(@NonNull executor: Executor, @NonNull callback: MediaPlayer.PlayerCallback)

Register PlayerCallback to listen changes.

ListenableFuture<SessionPlayer.PlayerResult!>
removePlaylistItem(@IntRange(0) index: Int)

Removes the media item from the playlist

ListenableFuture<SessionPlayer.PlayerResult!>
replacePlaylistItem(index: Int, @NonNull item: MediaItem)

Replaces the media item at index in the playlist.

Unit

Resets MediaPlayer to its uninitialized state if not closed.

ListenableFuture<SessionPlayer.PlayerResult!>
seekTo(position: Long)

Seeks to the specified position.

ListenableFuture<SessionPlayer.PlayerResult!>
seekTo(position: Long, mode: Int)

Moves the media to specified time position by considering the given mode.

ListenableFuture<SessionPlayer.PlayerResult!>
selectTrack(@NonNull trackInfo: SessionPlayer.TrackInfo)

Selects the TrackInfo for the current media item.

ListenableFuture<SessionPlayer.PlayerResult!>
selectTrack(@NonNull trackInfo: MediaPlayer.TrackInfo)

ListenableFuture<SessionPlayer.PlayerResult!>

Sets the AudioAttributesCompat to be used during the playback of the media.

ListenableFuture<SessionPlayer.PlayerResult!>
setAudioSessionId(sessionId: Int)

Sets the audio session ID.

ListenableFuture<SessionPlayer.PlayerResult!>
setAuxEffectSendLevel(@FloatRange(0, 1) level: Float)

Sets the send level of the player to the attached auxiliary effect.

ListenableFuture<SessionPlayer.PlayerResult!>
setMediaItem(@NonNull item: MediaItem)

Sets a MediaItem for playback.

ListenableFuture<SessionPlayer.PlayerResult!>

Sets playback params using PlaybackParams.

ListenableFuture<SessionPlayer.PlayerResult!>
setPlaybackSpeed(@FloatRange(0.0, 3.4028235E38, false) playbackSpeed: Float)

Sets the playback speed.

ListenableFuture<SessionPlayer.PlayerResult!>
setPlayerVolume(@FloatRange(0, 1) volume: Float)

Sets the volume of the audio of the media to play, expressed as a linear multiplier on the audio samples.

ListenableFuture<SessionPlayer.PlayerResult!>
setPlaylist(@NonNull list: MutableList<MediaItem!>, @Nullable metadata: MediaMetadata?)

Sets a list of MediaItem with metadata.

ListenableFuture<SessionPlayer.PlayerResult!>
setRepeatMode(repeatMode: Int)

Sets the repeat mode.

ListenableFuture<SessionPlayer.PlayerResult!>
setShuffleMode(shuffleMode: Int)

Sets the shuffle mode.

ListenableFuture<SessionPlayer.PlayerResult!>
setSurface(@Nullable surface: Surface?)

Sets the Surface to be used as the sink for the video portion of the media.

ListenableFuture<SessionPlayer.PlayerResult!>

Skips to the next item in the playlist.

ListenableFuture<SessionPlayer.PlayerResult!>
skipToPlaylistItem(@IntRange(0) index: Int)

Skips to the item in the playlist at the index.

ListenableFuture<SessionPlayer.PlayerResult!>

Skips to the previous item in the playlist.

Unit

Unregister the previously registered PlayerCallback.

ListenableFuture<SessionPlayer.PlayerResult!>
updatePlaylistMetadata(@Nullable metadata: MediaMetadata?)

Updates the playlist metadata while keeping the playlist as-is.

Inherited functions

Constants

MEDIA_INFO_AUDIO_NOT_PLAYING

static val MEDIA_INFO_AUDIO_NOT_PLAYING: Int

Informs that audio is not playing. Note that playback of the video is not interrupted.

Value: 804

MEDIA_INFO_BAD_INTERLEAVING

static val MEDIA_INFO_BAD_INTERLEAVING: Int

Bad interleaving means that a media has been improperly interleaved or not interleaved at all, e.g has all the video samples first then all the audio ones. Video is playing but a lot of disk seeks may be happening.

Value: 800

MEDIA_INFO_BUFFERING_UPDATE

static val MEDIA_INFO_BUFFERING_UPDATE: Int

Update status in buffering a media source received through progressive downloading. The received buffering percentage indicates how much of the content has been buffered or played. For example a buffering update of 80 percent when half the content has already been played indicates that the next 30 percent of the content to play has been buffered.

The extra parameter in PlayerCallback#onInfo is the percentage (0-100) of the content that has been buffered or played thus far.

Value: 704

MEDIA_INFO_METADATA_UPDATE

static val MEDIA_INFO_METADATA_UPDATE: Int

A new set of metadata is available.

Value: 802

MEDIA_INFO_NOT_SEEKABLE

static val MEDIA_INFO_NOT_SEEKABLE: Int

The media cannot be seeked (e.g live stream)

Value: 801

MEDIA_INFO_VIDEO_NOT_PLAYING

static val MEDIA_INFO_VIDEO_NOT_PLAYING: Int

Informs that video is not playing. Note that playback of the audio is not interrupted.

Value: 805

MEDIA_INFO_VIDEO_RENDERING_START

static val MEDIA_INFO_VIDEO_RENDERING_START: Int

The player just pushed the very first video frame for rendering.

Value: 3

MEDIA_INFO_VIDEO_TRACK_LAGGING

static val MEDIA_INFO_VIDEO_TRACK_LAGGING: Int

The video is too complex for the decoder: it can't decode frames fast enough. Possibly only the audio plays fine at this stage.

Value: 700

NO_TRACK_SELECTED

static val NO_TRACK_SELECTED: Int

Deprecated: getSelectedTrack(int) returns null instead of this value.

The return value of getSelectedTrack(int) when there is no selected track for the given type.

Value: Integer.MIN_VALUE

PLAYER_ERROR_IO

static val PLAYER_ERROR_IO: Int

File or network related operation errors.

Value: -1004

PLAYER_ERROR_MALFORMED

static val PLAYER_ERROR_MALFORMED: Int

Bitstream is not conforming to the related coding standard or file spec.

Value: -1007

PLAYER_ERROR_TIMED_OUT

static val PLAYER_ERROR_TIMED_OUT: Int

Some operation takes too long to complete, usually more than 3-5 seconds.

Value: -110

PLAYER_ERROR_UNKNOWN

static val PLAYER_ERROR_UNKNOWN: Int

Unspecified player error.

Value: 1

PLAYER_ERROR_UNSUPPORTED

static val PLAYER_ERROR_UNSUPPORTED: Int

Bitstream is conforming to the related coding standard or file spec, but the media framework does not support the feature.

Value: -1010

See Also