iOS 14 AVCaptureSession 捕获立体声音频与视频录制
iOS 14 AVCaptureSession capturing stereo audio with video recording
我想知道是否有一种方法可以使用 AVCaptureSession 捕获立体声音频。 iOS14为AVAudioRecorder增加了对立体声音频的支持,但到目前为止我一直没有成功通过AVCaptureSession捕获立体声音频。
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:NULL];
NSArray *portDescriptions = audioSession.availableInputs;
AVAudioSessionPortDescription *builtInMicPort = nil;
for (AVAudioSessionPortDescription *port in portDescriptions) {
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
builtInMicPort = port;
break;
}
} // end input iteration
[audioSession setPreferredInput:builtInMicPort error:nil];
NSArray *supportedPolarPatterns = audioSession.preferredInput.dataSources.firstObject.supportedPolarPatterns;
[audioSession.preferredInput.dataSources.firstObject setPreferredPolarPattern:AVAudioSessionPolarPatternStereo error:nil];
[audioSession.preferredInput setPreferredDataSource:audioSession.preferredInput.dataSources.firstObject error:nil];
[audioSession setPreferredInputOrientation:AVAudioStereoOrientationPortrait error:nil];
device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if([videoSession canAddInput:deviceInput]){
[videoSession addInput:deviceInput];
}
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
if([videoSession canAddInput:audioDeviceInput]){
[videoSession addInput:audioDeviceInput];
}
试试这个:
videoSession.usesApplicationAudioSession = true
videoSession.automaticallyConfiguresApplicationAudioSession = false
我能够在 iOS 14 中使用此添加的立体声。
这是工作代码:
//STEREO AUDIO RECORDING
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:NULL];
// select the built-in Back mic if available
NSArray *portDescriptions = audioSession.availableInputs;
AVAudioSessionPortDescription *builtInMicPort = nil;
for (AVAudioSessionPortDescription *port in portDescriptions) {
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
builtInMicPort = port;
break;
}
} // end input iteration
[audioSession setPreferredInput:builtInMicPort error:nil];
AVAudioSessionDataSourceDescription* frontDataSource = nil;
for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources)
{
if ([source.orientation isEqual:AVAudioSessionOrientationFront])
{
frontDataSource = source;
break;
}
} // end data source iteration
NSArray *supportedPolarPatterns = frontDataSource.supportedPolarPatterns;
NSLog(@"SUPPORTED POLAR PATTERNS : %@", supportedPolarPatterns);
BOOL isStereoSupported = [supportedPolarPatterns containsObject:@"Stereo"];
if(isStereoSupported == YES){
if(@available(iOS 14.0, *)) {
[frontDataSource setPreferredPolarPattern:AVAudioSessionPolarPatternStereo error:nil];
} else {
// Fallback on earlier versions
}
}
[audioSession.preferredInput setPreferredDataSource:frontDataSource error:nil];
if(@available(iOS 14.0, *)) {
[audioSession setPreferredInputOrientation:AVAudioStereoOrientationPortrait error:nil];
}
videoSession = [[AVCaptureSession alloc] init];
//STEREO AUDIO RECORDING
videoSession.usesApplicationAudioSession = YES;
videoSession.automaticallyConfiguresApplicationAudioSession = NO;
我想知道是否有一种方法可以使用 AVCaptureSession 捕获立体声音频。 iOS14为AVAudioRecorder增加了对立体声音频的支持,但到目前为止我一直没有成功通过AVCaptureSession捕获立体声音频。
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:NULL];
NSArray *portDescriptions = audioSession.availableInputs;
AVAudioSessionPortDescription *builtInMicPort = nil;
for (AVAudioSessionPortDescription *port in portDescriptions) {
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
builtInMicPort = port;
break;
}
} // end input iteration
[audioSession setPreferredInput:builtInMicPort error:nil];
NSArray *supportedPolarPatterns = audioSession.preferredInput.dataSources.firstObject.supportedPolarPatterns;
[audioSession.preferredInput.dataSources.firstObject setPreferredPolarPattern:AVAudioSessionPolarPatternStereo error:nil];
[audioSession.preferredInput setPreferredDataSource:audioSession.preferredInput.dataSources.firstObject error:nil];
[audioSession setPreferredInputOrientation:AVAudioStereoOrientationPortrait error:nil];
device = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInWideAngleCamera mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionBack];
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
if([videoSession canAddInput:deviceInput]){
[videoSession addInput:deviceInput];
}
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
if([videoSession canAddInput:audioDeviceInput]){
[videoSession addInput:audioDeviceInput];
}
试试这个:
videoSession.usesApplicationAudioSession = true
videoSession.automaticallyConfiguresApplicationAudioSession = false
我能够在 iOS 14 中使用此添加的立体声。
这是工作代码:
//STEREO AUDIO RECORDING
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
[audioSession setActive:YES error:NULL];
// select the built-in Back mic if available
NSArray *portDescriptions = audioSession.availableInputs;
AVAudioSessionPortDescription *builtInMicPort = nil;
for (AVAudioSessionPortDescription *port in portDescriptions) {
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic]) {
builtInMicPort = port;
break;
}
} // end input iteration
[audioSession setPreferredInput:builtInMicPort error:nil];
AVAudioSessionDataSourceDescription* frontDataSource = nil;
for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources)
{
if ([source.orientation isEqual:AVAudioSessionOrientationFront])
{
frontDataSource = source;
break;
}
} // end data source iteration
NSArray *supportedPolarPatterns = frontDataSource.supportedPolarPatterns;
NSLog(@"SUPPORTED POLAR PATTERNS : %@", supportedPolarPatterns);
BOOL isStereoSupported = [supportedPolarPatterns containsObject:@"Stereo"];
if(isStereoSupported == YES){
if(@available(iOS 14.0, *)) {
[frontDataSource setPreferredPolarPattern:AVAudioSessionPolarPatternStereo error:nil];
} else {
// Fallback on earlier versions
}
}
[audioSession.preferredInput setPreferredDataSource:frontDataSource error:nil];
if(@available(iOS 14.0, *)) {
[audioSession setPreferredInputOrientation:AVAudioStereoOrientationPortrait error:nil];
}
videoSession = [[AVCaptureSession alloc] init];
//STEREO AUDIO RECORDING
videoSession.usesApplicationAudioSession = YES;
videoSession.automaticallyConfiguresApplicationAudioSession = NO;