如何防止Callkit停止AVAudioPlayer?

时间:2018-03-29 01:06:17

标签: ios swift twilio avaudioplayer

我有一个使用Twilio的voip视频聊天应用程序,我试图为拨打电话的用户播放回铃音。但是每当调用didActivate:audioSession时,我的AVAudioPlayer都会停止,导致回铃音仅播放片刻。在Callkit激活音频会话时,如何继续播放回铃音?这是我的代码:

func configure() {
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, mode: AVAudioSessionModeVideoChat, options: [.mixWithOthers, .allowBluetooth, .defaultToSpeaker])
        } catch {
            print(error.localizedDescription)
        }

        let configuration = CXProviderConfiguration(localizedName: "Thirty")
        configuration.maximumCallGroups = 1
        configuration.maximumCallsPerCallGroup = 1
        configuration.supportsVideo = true
        configuration.supportedHandleTypes = [.generic]
        if let callKitIcon = UIImage(named: "callkitIcon") {
            configuration.iconTemplateImageData = UIImagePNGRepresentation(callKitIcon)
        }    
        callKitProvider = CXProvider(configuration: configuration)
        callKitProvider?.setDelegate(self, queue: nil)

        let path = Bundle.main.path(forResource: "ringbackMorningSong.mp3", ofType:nil)!
        let url = URL(fileURLWithPath: path)
        ringbackAudioPlayer = try? AVAudioPlayer(contentsOf: url)
        ringbackAudioPlayer?.numberOfLoops = -1
        ringbackAudioPlayer?.prepareToPlay()
    }

然后当用户点击"拨打电话"按钮,ringbackAudioPlayer开始播放。

然后执行CXStartCallAction。然后调用didActivate:audioSession,这就是我的回铃音停止的地方。

1 个答案:

答案 0 :(得分:0)

可能不是最好的解决方案,但我通过在participantDidConnect 方法(twilio)中添加CXStartCallAction(call: uuid, handle: callHandle) 解决了这个问题。这样,AVAudioPlayer 就不会被 CallKit 打断,其他用户连接后就不需要 AVAudioPlayer 的声音了。