I'm developing an app that should have the following properties regarding the audio:
- can record and play sound at the same time
- can mix the audio output with other app, e.g. iPod
- audio output at speaker when earphone is not plugged in
- audio output at earphone when it is plugged in
I used the following code.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *audioSessionError;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];
UInt32 mix = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mix), &mix);
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSession开发者_Python百科SetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
[audioSession setActive:YES error:&audioSessionError];
However, I can achieve 1-3 but failed at 4. When earphone is plugged in, the audio still comes through the speaker. Then I tried setting kAudioSessionProperty_OverrideCategoryDefaultToSpeaker
instead of kAudioSessionProperty_OverrideAudioRoute
, but this resulted pausing the iPod instead of mixing both audio. Could anyone please help pointing out what's wrong with the above code?
Thanks for any help.
I think this:
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
Specifically means use the speaker. The default action (to use the headphones when plugged in) should be:
UInt32 route = kAudioSessionOverrideAudioRoute_None;
精彩评论