Search code examples
androidaudio

AudioTrack. How can I use sessionId?


Documentations says: sessionId - Id of audio session the AudioTrack must be attached to

May I use it something like this?:

MediaPlayer mp = MediaPlayer.create(this, R.raw.test);
            mp.start();

int minSize = AudioTrack.getMinBufferSize(
        44100, AudioFormat.CHANNEL_OUT_STEREO, 
        AudioFormat.ENCODING_PCM_16BIT );

    at = new AudioTrack(AudioManager.STREAM_MUSIC, 
                        44100, AudioFormat.CHANNEL_OUT_STEREO, 
                        AudioFormat.ENCODING_PCM_16BIT, minSize, 
                        AudioTrack.MODE_STREAM, mp.getAudioSessionId());

    at.setStereoVolume(0.0f, 1.0f);  

What is right way to connect audio track to stream which used to media player. Can I make changes on this stream using AudioTrack?


Solution

  • You don't need to specify a session ID, as there's also an AudioTrack constructor that doesn't have a sessionId input parameter. However, as the documentation states, if you want to "associate audio effects to a particular instance of AudioTrack" you should use the constructor that takes a session ID.
    This session ID can either be taken from a MediaPlayer instance that you have created - or it can be zero, in which case "a new session will be created for this track if none is supplied".