如何判断录音源

发布于 2024-11-09 22:34:58 字数 80 浏览 2 评论 0原文

使用 iPad 应用程序录制声音时,如何知道声音来源是来自内置麦克风还是耳机麦克风?

其他信息:iOS 版本 4.2 及更高版本。

While recording a sound using my iPad app, how can I know if the source of the sound is from the built-in microphone or headphone microphone?

Additional information: iOS ver 4.2 and above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-11-16 22:34:58

确定这一点的方法是轮询硬件并查询当前的音频路由。

使用 AudioSessionGetProperty 对象获取返回音频的路线。

这个@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];
    }
}

The way to determine this is to poll the hardware and query the current audio route.

Use the AudioSessionGetProperty object to get back the route of the audio.

This example by @TPoschel should set you on the right track.

- (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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文