如何录制视频并保持后台播放音乐?

How to record video and maintain music playing in the background?

抱歉,如果这是重复的,但我找不到与此类似的问题。我有一个用 AVFoundation 制作的自定义 camera/recorder,我想知道如何在录制视频时保持其他应用程序的音频 运行,因为现在它会停止音频(甚至不会暂停)然后录制视频

如果我的想法是正确的,是否可以通过添加类似于此的内容来解决这个问题:

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

示例代码:

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];
This fixed it.


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