ios10 iphone5s voip siphon pjsip2.5.5 Error opening sound device

陌路散爱 提交于 2019-12-06 10:52:22

Step 1: At first open info.plist file in your xcode project in Supporting Files Folder and click "+" in Information property list.

Step 2: Select and Add Privacy - Microphone Usage Description in the list.

Step 3: Add the value Microphone in the Privacy - Microphone Usage Description.

Now Compile and run your project.

At last,I fix this bug with IOS's CallKit,take a look at this website: https://trac.pjsip.org/repos/ticket/1941

Details: CallKit framework allows apps to use the native phone UI to receive incoming calls and make outgoing calls. In order to achieve this, CallKit requires the call audio to start only when audio session has been activated, thus it's recommended that when using PJSIP, you open the sound device only when necessary. It can be done by:

Starting PJSIP with no sound device (by calling pjsua_set_no_snd_dev() after startup). Close the sound device when unused, also with the same API (pjsua_set_no_snd_dev()). Upon audio session activation, open the sound device with the API pjsua_set_snd_dev(). Below is an example on how to integrate CallKit with PJSIP, with the delegate functions taken from ​Speakerbox sample app provided by Apple.

To make outgoing call:

func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
        /* 1. We must not start call audio here, and can only do so
         *    once the audio session has been activated by the system
         *    after having its priority elevated. So, make sure that the sound
         *    device is closed at this point.
         */

        /* 2. Provide your own implementation to configure
         *    the audio session here.
         */
        configureAudioSession()

        /* 3. Make call with pjsua_call_make_call().
         *    Then use pjsua's on_call_state() callback to report significant
         *    events in the call's lifecycle, by calling iOS API
         *    CXProvider.reportOutgoingCall(with: startedConnectingAt:) and
         *    CXProvider.reportOutgoingCall(with: ConnectedAt:)
         */

        /* 4. If step (3) above returns PJ_SUCCESS, call action.fulfill(),
         *    otherwise call action.fail().
         */
    }

To handle incoming call:

 func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
        /* 1. We must not start call audio here, and can only do so
         *    once the audio session has been activated by the system
         *    after having its priority elevated. So, make sure that the sound
         *    device is closed at this point.
         */

        /* 2. Provide your own implementation to configure
         *    the audio session here.
         */
        configureAudioSession()

        /* 3. Answer the call with pjsua_call_answer().
         */

        /* 4. If step (3) above returns PJ_SUCCESS, call action.fulfill(),
         *    otherwise call action.fail().
         */
    }

To start sound device:

func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    /* Start call audio media, now that the audio session has been
     * activated after having its priority boosted.
     *
     * Call pjsua API pjsua_set_snd_dev() here.
     */
}

My issue was that, when I played a sound on incoming calls I set the AudioSession caterogry Playback like:

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)

I changed it to Play and record:

try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)

and it worked after that. So if you somewhere in your code set the audio session to playback, change it to play and record.

@Altanai

func setAudioSessionActive(active: Bool) -> Bool{
    let sessionInterface = AVAudioSession.sharedInstance()
    do{
        try sessionInterface.setActive(active)

        if(active){
            if(sessionInterface.category != AVAudioSessionCategoryPlayAndRecord){
                try sessionInterface.setCategory(AVAudioSessionCategoryPlayAndRecord)
            }
            if(sessionInterface.mode != AVAudioSessionModeVoiceChat){
                try sessionInterface.setMode(AVAudioSessionModeVoiceChat)
            }
        }
        return true
    }
    catch let error{
        return false
    }
}

func configureAudioSession(){
    let sessionInterface = AVAudioSession.sharedInstance()

    do{
        if(sessionInterface.responds(to: #selector(AVAudioSession.setCategory(_:with:)))){
            try sessionInterface.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
        }
        else{
            try sessionInterface.setCategory(AVAudioSessionCategoryPlayAndRecord)
        }
        try sessionInterface.setMode(AVAudioSessionModeVoiceChat)

    }
    catch let error{
        NSLog("SipManager - configureAudioSession error: \(error)")
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!