开发者

iPhone AudioSession properties

开发者 https://www.devze.com 2023-01-08 14:13 出处:网络
I\'m developing an app that should have the following properties regarding the audio: can record and play sound at the same time

I'm developing an app that should have the following properties regarding the audio:

  1. can record and play sound at the same time
  2. can mix the audio output with other app, e.g. iPod
  3. audio output at speaker when earphone is not plugged in
  4. 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消