-->

How to record video and maintain music playing in

2019-03-28 09:53发布

问题:

Sorry if this is a duplicate, but I couldn't find a question similar to this. I have a custom camera/recorder that I made with AVFoundation and I was wondering how to keep the audio running from other apps while recording a video because right now it stops the audio (doesn't even pause it) and then records the video

If I am thinking correctly, could this be solved by adding something similar to this:

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

回答1:

Sample Code:

AVAudioSession *session = [AVAudioSession sharedInstance];
session.delegate = self;

NSError *error = nil;
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];

OSStatus propertySetError = 0;

UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
propertySetError = AudioSessionSetProperty ( 
    kAudioSessionProperty_OverrideAudioRoute,                         
    sizeof (audioRouteOverride),                                      
    &audioRouteOverride                                               
); 
[session setActive:YES error:&error];


回答2:

This fixed it.


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