Okay so I’ve tried to look this up so it isn’t a duplicate but I might have missed something. Anyways, in my app I have a song that should start playing when I hit record. So when I hit record, the AVAudioRecorder starts recording and my already initialized AVAudioPlayer starts playing the song. Yet the song’s volume becomes very quiet. I know it isn’t the song because if I simply play the song without attempting to record at the same time, it plays at full volume. Any help guys? Thanks.
How I’m Initializing:
NSDictionary *recordSettings = [NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityMin],
AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16],
AVEncoderBitRateKey,
[NSNumber numberWithInt: 2],
AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44010.0],
AVSampleRateKey,
nil];
NSError *error = nil;
audioRecorder = [[AVAudioRecorder alloc]
initWithURL:soundFileURL
settings:recordSettings
error:&error];
if (error)
{
NSLog(@"error: %@", [error localizedDescription]);
} else {
[audioRecorder prepareToRecord];
}
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:"Rang De"
ofType:@"mp3"]];
audioPlayerForPreloadedMusic = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else
{
audioPlayerForPreloadedMusic.delegate = self;
[audioPlayerForPreloadedMusic prepareToPlay];
}
How I’m Playing:
-(void) recordAudio
{
if (!audioRecorder.recording)
{
playButton.enabled = NO;
stopButton.enabled = YES;
[audioRecorder record];
if(!audioPlayerForPreloadedMusic.playing)
[audioPlayerForPreloadedMusic play];
else {
NSLog(@"music is playing, so won't play");
}
}
}
You are playing the song out the earpiece when you start recording, instead of out of the speaker.
There is an audio session override to prevent this automatic switch. See using: kAudioSessionProperty_OverrideAudioRoute and kAudioSessionOverrideAudioRoute_Speaker in Apple’s iOS API documentation.