AVCaptureSession and background audio iOS 7

拟墨画扇 提交于 2019-11-27 01:09:36
KNaito

You can use the AVAudioSessionCategoryOptionMixWithOthers. For instance,

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

After that, you can use the AVAudioPlayer simultaneously with AVCaptureSession.

However, the above code leads to very low volume. If you want normal volume, use the AVAudioSessionCategoryOptionDefaultToSpeaker with AVAudioSessionCategoryOptionMixWithOthers as follows,

[session setCategory:AVAudioSessionCategoryPlayAndRecord  withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

This goes well.

In your AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Stop app pausing other sound.
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                     withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionDefaultToSpeaker
                                           error:nil];
}

Where you are allocating AVCaptureSession:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
session.automaticallyConfiguresApplicationAudioSession = NO;

This code will allow you to play background music and run AVCaptureSession with the microphone.

SWIFT UPDATE:

AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Stop app pausing other sound.
    do{
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, 
                                            withOptions: [.DuckOthers, .DefaultToSpeaker])
    }
    catch {

    }

    return true
}

Where you are allocating AVCaptureSession:

let session = AVCaptureSession()
session.automaticallyConfiguresApplicationAudioSession = false
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!