如何确定录音源
Posted
技术标签:
【中文标题】如何确定录音源【英文标题】:How to determine sound recording source 【发布时间】:2011-05-25 12:39:47 【问题描述】:在使用我的 iPad 应用录制声音时,我如何知道声音的来源是来自内置麦克风还是耳机麦克风?
附加信息:ios 4.2 及更高版本。
【问题讨论】:
【参考方案1】:确定的方法是轮询硬件,查询当前音频路由。
使用AudioSessionGetProperty 对象获取音频的路由。
这个example by @TPoschel 应该会让你走上正轨。
- (void)playSound:(id) sender
if(player)
CFStringRef route;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
if((route == NULL) || (CFStringGetLength(route) == 0))
// Silent Mode
NSLog(@"AudioRoute: SILENT");
else
NSString* routeStr = (NSString*)route;
NSLog(@"AudioRoute: %@", routeStr);
/* Known values of route:
* "Headset"
* "Headphone"
* "Speaker"
* "SpeakerAndMicrophone"
* "HeadphonesAndMicrophone"
* "HeadsetInOut"
* "ReceiverAndMicrophone"
* "Lineout"
*/
NSRange headphoneRange = [routeStr rangeOfString : @"Headphone"];
NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];
NSRange speakerRange = [routeStr rangeOfString : @"Speaker"];
NSRange lineoutRange = [routeStr rangeOfString : @"Lineout"];
if (headphoneRange.location != NSNotFound)
// Don't change the route if the headphone is plugged in.
else if(headsetRange.location != NSNotFound)
// Don't change the route if the headset is plugged in.
else if (receiverRange.location != NSNotFound)
// Change to play on the speaker
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
else if (speakerRange.location != NSNotFound)
// Don't change the route if the speaker is currently playing.
else if (lineoutRange.location != NSNotFound)
// Don't change the route if the lineout is plugged in.
else
NSLog(@"Unknown audio route.");
[player play];
【讨论】:
以上是关于如何确定录音源的主要内容,如果未能解决你的问题,请参考以下文章
HTTP 的源端口是如何确定的? NAT中是不是有冲突? [关闭]
Windows 多显示器:当目标可用但不活动时,如何确定目标是不是物理连接到源?