当麦克风插入时,我的应用程序继续从内置麦克风录音(我想使用插入的麦克风)。我正在使用:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
我希望避免排队服务或任何更深层次的事情。
在做了一些研究(用苹果文档)后,我找不到用队列做这个的方法......
经过进一步的研究,我根本无法弄清楚如何做到这一点。它没有在任何苹果文档或任何常量等中提及。有一个名为“FiRe”的应用程序可以做到这一点,所以我知道它是可能的。
答案 0 :(得分:0)
我意识到这是一个老问题,但想到有人可能会从中受益。
也许我真的不理解你的问题。您是否希望能够使用内置麦克风进行录制,然后在插入时无缝切换到耳机录制?或者你只是很难从耳机录音?
我使用此代码自行调试路由问题。只需使用您的应用程序,插入耳机并观看控制台中记录的内容。您应确保连接的耳机导致“HeadsetInOut”路线更改。如果它只是一个“耳机”,它将在耳机中播放声音,但从内置麦克风录制。
我完全没有使用AudioQueues从耳机录制的问题。
void interruptionListener( void * inClientData,
UInt32 inInterruptionState)
{
if (inInterruptionState == kAudioSessionBeginInterruption) {
// handle interruption
}
else if (inInterruptionState == kAudioSessionEndInterruption) {
// handle interruption
}
}
void propListener( void * inClientData,
AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData)
{
FooBar *self = (__bridge FooBar*)inClientData;
if (inID == kAudioSessionProperty_AudioRouteChange) {
CFDictionaryRef routeDictionary = (CFDictionaryRef)inData;
CFNumberRef reason = (CFNumberRef)CFDictionaryGetValue(routeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_Reason));
SInt32 reasonVal;
CFNumberGetValue(reason, kCFNumberSInt32Type, &reasonVal);
if (reasonVal != kAudioSessionRouteChangeReason_CategoryChange) {
// Check the old route
CFStringRef oldRoute = (CFStringRef)CFDictionaryGetValue(routeDictionary, CFSTR(kAudioSession_AudioRouteChangeKey_OldRoute));
if (oldRoute) {
NSLog(@"Changed audio route from route:");
CFShow(oldRoute);
} else {
NSLog(@"Error getting old audio route.\n");
}
CFStringRef newRoute;
UInt32 size; size = sizeof(CFStringRef);
OSStatus error = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute);
if (error) {
NSLog(@"Error getting new audio route: %ld\n", error);
}
else {
NSLog(@"Changed audio route to:\n");
CFShow(newRoute);
if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), 0) == kCFCompareEqualTo)
{
// you can do some recording, yay.
}
}
if (reasonVal == kAudioSessionRouteChangeReason_OldDeviceUnavailable)
{
NSLog(@"kAudioSessionRouteChangeReason_OldDeviceUnavailable");
}
}
}
}
-(id) init
{
self = [super init];
if (self) {
OSStatus error = AudioSessionInitialize(NULL, NULL, interruptionListener, (__bridge void *)(self));
if (error)
{
NSLog(@"Error initializing audio session: %ld\n", error);
} else {
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, propListener, (__bridge void *)(self));
if (error) {
NSLog(@"Error adding audio session property listener (route change): %ld\n", error);
}
UInt32 inputAvailable = 0;
UInt32 size = sizeof(inputAvailable);
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) {
NSLog(@"Error getting input availability: %ld\n", error);
}
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, (__bridge void *)(self));
if (error) {
NSLog(@"Error adding audio session property listener (input availability): %ld\n", error);
}
// Activate the session
error = AudioSessionSetActive(true);
if (error) {
NSLog(@"Failed to activate audio session: %ld\n", error);
}
}
}
return self;
}