SpeakHere录音机应用扬声器不工作-iOS

时间:2013-07-10 02:20:01

标签: ios avaudiosession voice-recording

我在我的项目中使用Apple提供的SpeakHere示例代码。

如何启用扬声器如果没有插入耳机或耳机?

我知道我可以使用以下代码覆盖路线..

OSStatus error;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
error = AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
if (error) printf("couldn't set audio speaker!");

但我不希望这样,因为如果插入耳机,声音就会从它们中传出来。

2 个答案:

答案 0 :(得分:1)

这是从这里偷来/借用的耳机/配件检测方法Detect if headphones (not microphone) are plugged in to an iOS device

使用此方法以及使用下面方法的if语句可以获得结果。

 - (BOOL)isHeadsetPluggedIn {
    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                              &routeSize,
                                              &route);

    /* Known values of route:
     * "Headset"
     * "Headphone"
     * "Speaker"
     * "SpeakerAndMicrophone"
     * "HeadphonesAndMicrophone"
     * "HeadsetInOut"
     * "ReceiverAndMicrophone"
     * "Lineout"
     */

    if (!error && (route != NULL)) {

        NSString* routeStr = (NSString*)route;

        NSRange headphoneRange = [routeStr rangeOfString : @"Head"];

        if (headphoneRange.location != NSNotFound) return YES;

    }

    return NO;
}

答案 1 :(得分:1)

很可能这就是你要找的东西:

    UInt32 overrideValue = 1;
    AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(overrideValue), &overrideValue);

来自文档:指定在没有连接其他音频路径(如耳机)时是否将音频路由到扬声器(而不是接收器)。