Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the Android MediaPlayer audio session ID refer to?

As the title states, what does the MediaPlayer objects audio session ID refer to? Originally, I intend to retrieve the int resource ID of the audio resource my MediaPlayer is playing. But I couldn't find a method for that.

However, I stumbled upon this method getAudioSessionId() and I was wondering if that was the function I was looking for.

like image 958
brain56 Avatar asked Jan 17 '13 06:01

brain56


People also ask

How do I play music through MediaPlayer?

Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.

What is MediaPlayer release?

android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams.


Video Answer


2 Answers

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.

like image 187
Charles Munger Avatar answered Oct 14 '22 06:10

Charles Munger


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.

like image 20
mixel Avatar answered Oct 14 '22 06:10

mixel