Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For Android, you can use the AudioTrack class to adjust the audio balance by setting the output channels. You can use the following code to pan the audio to the left or right:

AudioTrack audioTrack = new AudioTrack.Builder()
        .setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build())
        .setAudioFormat(new AudioFormat.Builder()
                .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                .setSampleRate(44100)
                .setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
                .build())
        .setBufferSizeInBytes(minimumBufferSize)
        .build();

float panValue = -1.0f; // -1.0 (left) to 1.0 (right)
// 0.0 will be centered (L/R).
audioTrack.setStereoVolume(1.0f - Math.abs(panValue), 1.0f + panValue);

For iOS, you can use the AVAudioPlayer class to adjust the audio balance by setting the pan value. You can use the following code to pan the audio to the left or right:

// Create an audio player instance
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:@"mySound" withExtension:@"mp3"];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];

float panValue = -1.0f; // -1.0 (left) to 1.0 (right)
// 0.0 will be centered (L/R).
player.pan = panValue;
[player play];