Ask Your Question
0

What is the method for programmatically adjusting the audio balance (specifically panning between L/R) for Android/iOS?

asked 2021-12-01 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-04-24 09:00:00 +0000

djk gravatar image

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];
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-01 11:00:00 +0000

Seen: 8 times

Last updated: Apr 24 '22