MediaPlayer

public final class MediaPlayer
extends SessionPlayer

java.lang.Object
   ↳ 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 SessionPlayer.PLAYER_STATE_IDLE.

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

Audio AttributesAudio Focus Gain TypeMisc
AudioAttributesCompat.USAGE_VOICE_COMMUNICATION_SIGNALLING AudioManager.AUDIOFOCUS_NONE
AudioManager.AUDIOFOCUS_GAIN Developers should specific a proper usage instead of AudioAttributesCompat.USAGE_UNKNOWN
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE
AudioAttributesCompat.USAGE_ASSISTANCE_ACCESSIBILITY AudioManager.AUDIOFOCUS_GAIN_TRANSIENT if AudioAttributesCompat.CONTENT_TYPE_SPEECH, 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(AudioAttributesCompat), getAudioAttributes() will return null and the default