Google is committed to advancing racial equity for Black communities. See how.

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.