我意識到這是一個老問題,但有人可能會從中受益。
也許我並不真正瞭解你的問題。您是否希望能夠使用內置麥克風進行錄音,然後在插入耳機時無縫切換至耳機錄音?或者你只是努力從耳機錄音?
我使用此代碼來自己調試路由問題。只需使用您的應用程序,插入耳機並觀看控制檯中註銷的內容即可。您應該確保您連接的耳機導致「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;
}
奇怪,從我讀什麼在這裏我http://stackoverflow.com/questions/4002133/forcing-iphone-microphone-as-audio-input希望它在默認情況下自動進行切換。 – 2011-06-11 01:58:28