【问题标题】:iOS pjsip 2.2 loud speaker switch failsiOS pjsip 2.2 扬声器开关失败
【发布时间】:2014-08-13 16:12:29
【问题描述】:

在通话期间,我尝试使用 pjsip 2.2 库在 iOS 设备上将语音从内部扬声器切换到扬声器。它返回 TRUE 作为成功,但在物理上它不会改变声音的目的地。

我使用下一个代码

- (BOOL)setLoud:(BOOL)loud {
if (loud) {
    @try {

        pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_LOUDSPEAKER;

        pj_status_t pj_status =   pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
                                                        &route, PJ_TRUE);
        if (pj_status == PJ_SUCCESS) {
          return YES;
        }
        else
        {
            return NO;
        }

    }
    @catch (NSException *exception) {
        return NO;
    }
} else {
    @try {
        pjmedia_aud_dev_route route = PJMEDIA_AUD_DEV_ROUTE_EARPIECE;

        pj_status_t pj_status =   pjsua_snd_set_setting(PJMEDIA_AUD_DEV_CAP_OUTPUT_ROUTE,
                                                        &route, PJ_TRUE);
        if (pj_status == PJ_SUCCESS) {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    @catch (NSException *exception) {
        return NO;
    }
}
}

您能否建议我们如何进行这项工作?

【问题讨论】:

    标签: voip pjsip speaker speakerphone


    【解决方案1】:

    随着 iOS 7 的引入,您现在应该使用 AVAudioSession 来处理任何音频管理。我花了很长时间才终于让它工作,但我终于弄清楚了为什么我的音频没有自动路由到我的 iPhone 扬声器的问题。问题是当您接听电话时,pjsip 会自动覆盖我在接听电话之前执行的 AVAudioSessionPortOverride。要解决此问题,您只需接听电话后覆盖输出音频端口。

    为了让我的 VoIP 应用程序在后台模式下高效工作,我决定在名为 on_call_state 的自定义回调方法中处理音频路由。此方法 on_call_state 在调用状态发生变化时由 pjsip 调用。正如您在此处所读到的,http://www.pjsip.org/pjsip/docs/html/group__PJSIP__INV.htm,当呼叫状态发生变化时,您可以检查许多不同的标志。我在这个例子中使用的状态是 PJSIP_INV_STATE_CONNECTINGPJSIP_INV_STATE_DISCONNECTED

    PJSIP_INV_STATE_CONNECTING 在音频呼叫连接到另一个对等方时调用。

    PJSIP_INV_STATE_DISCONNECTED在与另一个对等方的音频通话结束时被调用。

    static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
    {
        pjsua_call_info ci;
    
        PJ_UNUSED_ARG(e);
    
        pjsua_call_get_info(call_id, &ci);
        PJ_LOG(3,(THIS_FILE, "Call %d state=%.*s", call_id,
                  (int)ci.state_text.slen,
                  ci.state_text.ptr));
        if (ci.state == PJSIP_INV_STATE_CONNECTING) {
            BOOL success;
            AVAudioSession *session = [AVAudioSession sharedInstance];
            NSError *error = nil;
    
            success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
                               withOptions:AVAudioSessionCategoryOptionMixWithOthers
                                     error:&error];
            if (!success) NSLog(@"AVAudioSession error setCategory: %@", [error localizedDescription]);
    
            success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
            if (!success) NSLog(@"AVAudioSession error overrideOutputAudioPort: %@", [error localizedDescription]);
    
            success = [session setActive:YES error:&error];
            if (!success) NSLog(@"AVAudioSession error setActive: %@", [error localizedDescription]);
        } else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
            BOOL success;
            AVAudioSession *session = [AVAudioSession sharedInstance];
            NSError *error = nil;
    
            success = [session setActive:NO error:&error];
            if (!success) NSLog(@"AVAudioSession error setActive: %@", [error localizedDescription]);
        }
    }
    

    【讨论】:

    • 嘿!你是一个裂缝!很好的解决方案!
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 2017-11-06
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多