Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioRecorder & AVAudioPlayer - Sound output on internal speaker, how to change?

Tags:

iphone

i have a problem with AVAudioRecorder and AVAudioPlayer.

when i use Player and Record at the same time (eg. for playing sound while recording) the sound is in the quiet internal Speaker. i searched stackoverflow and all i found was this code:

UInt32 *audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

But this doesn't help me :( When i copyPaste it, i got errors. What can i do to record and play the loud Speaker at the bottom?

I don't use anything like SCLister oder something...

Thanks in advance

Max

like image 562
Max Steinmeyer Avatar asked Jun 23 '10 18:06

Max Steinmeyer


People also ask

How do I record audio with AVAudioRecorder?

First you need to import the AVFoundation framework into your view controller. You will need to add three properties to your view controller: a button for the user to tap to start or stop recording, an audio session to manage recording, and an audio recorder to handle the actual reading and saving of data.

What is the Avaudiorecorderdelegate protocol?

A protocol that defines the methods to respond to audio recording events and encoding errors.

What is Avaudioengine?

An object that manages a graph of audio nodes, controls playback, and configures real-time rendering constraints.

What is Avsampleratekey?

A floating point value that represents the sample rate, in hertz.


1 Answers

This is a bit old, but this post helped me and I wanted to update it for anyone else who might need it in the future. The code posted at the top is correct - it will take the quiet audio that's being played through the phone speaker and route it to the loudspeaker at the bottom. There is a minor typo in the code, which is why it's giving errors. Here is the correct snippet which will solve this issue:

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

Make sure you also activate the audio session right after setting this, before creating your audio player/recorder:

[[AVAudioSession sharedInstance] setActive:YES error:nil];

Last, if you're going to be playing and recording at the same time you'll probably need to set the category and mixing functions too. Here's the entire snippet which will set the category, enable mixing, route the audio to the main speaker, and activate the session. You'll want to do this only once right after the app launches.

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);

NSLog(@"Mixing: %x", propertySetError); // This should be 0 or there was an issue somewhere

[[AVAudioSession sharedInstance] setActive:YES error:nil];

Hope that helps someone!

like image 179
Cory Imdieke Avatar answered Oct 21 '22 14:10

Cory Imdieke