これは古い質問だとわかりましたが、誰かがこれで恩恵を受ける可能性があると考えました。
多分私はあなたの問題を本当に理解していません。内蔵のマイクを使って録音できるようにしたいのですが、ヘッドセットの接続時にシームレスにヘッドセットから録音に切り替えたいのですか?ヘッドセットから録音するのに苦労しますか?
このコードを使用してルーティングの問題を自分でデバッグします。あなたのアプリで遊んで、ヘッドセットを差し込み、コンソールに何がログアウトしているかを見てみましょう。接続したヘッドセットが「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私は自動的にスイッチを作るために、デフォルトではそれを期待します。 –
実際には、録音を停止してルートの変更を認識するために録音を再開する必要があります – russbishop