What does the Android MediaPlayer audio session ID refer to?

The android system keeps track of currently playing or recording sounds (audio sessions) and other services can hook into them by referencing their audio session ID. The system mix (what comes out of the speakers) has an audio session ID of 0.

The system mix audio session ID 0 is deprecated now so you have to use getAudioSessionId().

In short, that's not what you're looking for unless you want to build a visualizer.

EDIT: Also, to anyone trying to use the getAudioSessionID() from AudioRecord with a Visualizer or something else, that doesn't work.


From AudioManager.generateAudioSessionId documentation:

An audio session identifier is a system wide unique identifier for a set of audio streams (one or more mixed together).

The primary use of the audio session ID is to associate audio effects to audio players, such as MediaPlayer or AudioTrack: all audio effects sharing the same audio session ID will be applied to the mixed audio content of the players that share the same audio session.

From MediaPlayer.setAudioSessionId documentation:

... if an audio session ID is provided when creating an audio effect, this effect will be applied only to the audio content of media players within the same audio session and not to the output mix. When created, a MediaPlayer instance automatically generates its own audio session ID. However, it is possible to force this player to be part of an already existing audio session by calling this method. This method must be called before one of the overloaded setDataSource methods.

To generate new audio session id:

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int audioSessionId = audioManager.generateAudioSessionId();

AudioManager.generateAudioSessionId() can return AudioManager.ERROR.

So check it before assigning it to MediaPlayer:

if (audioSessionId != AudioManager.ERROR) {
    mediaPlayer.setAudioSessionId(audioSessionId);
}

Also:

Note that the audio session ID is 0 only if a problem occured when the MediaPlayer was contructed.