【发布时间】:2014-07-07 19:52:30
【问题描述】:
对于一个应用,我们需要从两个不同的音频源进行录制。一个麦克风是一种特殊的(喉)麦克风,它带有与带麦克风的 iPhone 耳机相同的连接器。
在第二个通道上,我们想录制环境声音,最好的办法是在我们从喉部麦克风耳机录制的同时从 iPhone/iPad 的内置麦克风录制。
有什么办法可以做到吗?还有其他提示吗?
【问题讨论】:
标签: ios iphone ipad core-audio audio-recording
对于一个应用,我们需要从两个不同的音频源进行录制。一个麦克风是一种特殊的(喉)麦克风,它带有与带麦克风的 iPhone 耳机相同的连接器。
在第二个通道上,我们想录制环境声音,最好的办法是在我们从喉部麦克风耳机录制的同时从 iPhone/iPad 的内置麦克风录制。
有什么办法可以做到吗?还有其他提示吗?
【问题讨论】:
标签: ios iphone ipad core-audio audio-recording
操作系统目前只允许应用一次连接到一个音频源路由。在现有的 iOS 设备上录制 2 通道的唯一方法是使用带有标准 USB 立体声 ADC 或具有多个麦克风输入的音频混合面板的 Apple USB 到 Lightning 连接器(旧型号上的相机连接套件)。
【讨论】:
我在 Apple library 上找到了一些关于如何从不同的麦克风端口选择数据源的常见问题解答,也许这些会有所帮助:
https://developer.apple.com/library/ios/qa/qa1799/_index.html
iOS 7 在选择特定内置麦克风方面为开发人员提供了更大的灵活性。
使用 iOS 7 中引入的 API,开发人员可以执行诸如定位代表内置麦克风的端口描述、定位特定麦克风(如“前”、“后”或“底部”)、设置您选择的麦克风等任务作为首选数据源,将内置麦克风端口设置为首选输入,如果硬件支持,甚至可以选择首选麦克风极性模式。请参阅 AVAudioSession.h。
清单 1 演示了应用程序如何找到代表内置麦克风的 AVAudioSessionPortDescription、定位前置麦克风(在 iPhone 5 或其他具有前置麦克风的设备上)、将前置麦克风设置为首选数据源并设置内置麦克风端口作为首选输入。
清单 1 演示输入选择。
#import <AVFoundation/AVAudioSession.h>
- (void) demonstrateInputSelection
{
NSError* theError = nil;
BOOL result = YES;
AVAudioSession* myAudioSession = [AVAudioSession sharedInstance];
result = [myAudioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&theError];
if (!result)
{
NSLog(@"setCategory failed");
}
result = [myAudioSession setActive:YES error:&theError];
if (!result)
{
NSLog(@"setActive failed");
}
// Get the set of available inputs. If there are no audio accessories attached, there will be
// only one available input -- the built in microphone.
NSArray* inputs = [myAudioSession availableInputs];
// Locate the Port corresponding to the built-in microphone.
AVAudioSessionPortDescription* builtInMicPort = nil;
for (AVAudioSessionPortDescription* port in inputs)
{
if ([port.portType isEqualToString:AVAudioSessionPortBuiltInMic])
{
builtInMicPort = port;
break;
}
}
// Print out a description of the data sources for the built-in microphone
NSLog(@"There are %u data sources for port :\"%@\"", (unsigned)[builtInMicPort.dataSources count], builtInMicPort);
NSLog(@"%@", builtInMicPort.dataSources);
// loop over the built-in mic's data sources and attempt to locate the front microphone
AVAudioSessionDataSourceDescription* frontDataSource = nil;
for (AVAudioSessionDataSourceDescription* source in builtInMicPort.dataSources)
{
if ([source.orientation isEqual:AVAudioSessionOrientationFront])
{
frontDataSource = source;
break;
}
} // end data source iteration
if (frontDataSource)
{
NSLog(@"Currently selected source is \"%@\" for port \"%@\"", builtInMicPort.selectedDataSource.dataSourceName, builtInMicPort.portName);
NSLog(@"Attempting to select source \"%@\" on port \"%@\"", frontDataSource, builtInMicPort.portName);
// Set a preference for the front data source.
theError = nil;
result = [builtInMicPort setPreferredDataSource:frontDataSource error:&theError];
if (!result)
{
// an error occurred. Handle it!
NSLog(@"setPreferredDataSource failed");
}
}
// Make sure the built-in mic is selected for input. This will be a no-op if the built-in mic is
// already the current input Port.
theError = nil;
result = [myAudioSession setPreferredInput:builtInMicPort error:&theError];
if (!result)
{
// an error occurred. Handle it!
NSLog(@"setPreferredInput failed");
}
}
在 iPhone 5 上运行时,清单 1 将产生以下控制台输出:
There are 3 data sources for port :"<AVAudioSessionPortDescription: 0x14d935a0, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Bottom>"
(
"<AVAudioSessionDataSourceDescription: 0x14d93800, ID = 1835216945; name = Bottom>",
"<AVAudioSessionDataSourceDescription: 0x14d938d0, ID = 1835216946; name = Front>",
"<AVAudioSessionDataSourceDescription: 0x14d93a10, ID = 1835216947; name = Back>"
)
Currently selected source is "Bottom" for port "iPhone Microphone"
Attempting to select source "<AVAudioSessionDataSourceDescription: 0x14d938d0, ID = 1835216946; name = Front>" on port "iPhone Microphone”
UPDATE 14 Nov
使用前面的代码我可以设置iPhone上的特定内置麦克风来录制声音,现在我正在尝试频繁更换iPhone上的特定麦克风来模拟立体声录音。
【讨论】: