SudheeR Bolla
SudheeR Bolla

Reputation: 171

How to switch audio output modes between stereo and mono in android programmatically?

Actually I am trying to make a custom music player application,in which i just wanted to provide users a chance to change audio output to stereo and mono.

Upvotes: 1

Views: 1660

Answers (1)

Kae10
Kae10

Reputation: 669

You can set when you make a instance of AudioTrack.

    /* Create AudioTrack instance */
    AudioTrack mAudioTrack = null;

    int minSize = AudioTrack.getMinBufferSize(
            16000,
            AudioFormat.CHANNEL_OUT_MONO, // or set AudioFormat.CHANNEL_OUT_STEREO
            AudioFormat.ENCODING_PCM_16BIT);

    if (mAudioTrack == null) {
        mAudioTrack = new AudioTrack(
                AudioManager.STREAM_MUSIC,
                16000,
                AudioFormat.CHANNEL_OUT_MONO, // or set AudioFormat.CHANNEL_OUT_STEREO
                AudioFormat.ENCODING_PCM_16BIT,
                minSize,
                AudioTrack.MODE_STREAM);
    }
    /* Create AudioTrack instance */

    /* Stop and release AudioTrack instance */
    mAudioTrack.flush();
    mAudioTrack.stop();
    mAudioTrack.release();
    mAudioTrack = null;
    /* Stop and release AudioTrack instance */

If you want to make new instance of AudioTrack in runtime, previous instance will be released first.

Upvotes: 2

Related Questions