From the AudioManager.generateAudioSessionId documentation:
An audio session identifier is a unique system identifier for a set of audio streams (one or more mixed together).
The main use of an audio messaging identifier is to associate sound effects with audio players such as MediaPlayer or AudioTrack: all audio effects sharing the audio session identifier will be applied to mixed audio content that share the same audio session.
From MediaPlayer.setAudioSessionId Document :
... if an audio session identifier is provided when creating the sound effect, this effect will be applied only to the audio content of the media players within the same audio session, and not the output mix. When created, the MediaPlayer instance automatically generates its own audio session I would. However, you can force this player to be part of an existing audio recording by calling this method. This method must be called before one of the overloaded setDataSource methods.
To create a new audio session identifier:
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int audioSessionId = audioManager.generateAudioSessionId();
AudioManager.generateAudioSessionId() can return AudioManager.ERROR .
So check it out before assigning it to MediaPlayer :
if (audioSessionId != AudioManager.ERROR) { mediaPlayer.setAudioSessionId(audioSessionId); }
Also :
Please note that the audio session identifier is 0 only if there was a problem creating MediaPlayer.
mixel source share