我有一个使用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()
}
当用户点击“拨打电话”按钮时,铃声播放器开始播放。
随后执行
CXStartCallAction
操作。然后调用 didActivate:audioSession
方法,在此处停止我的回铃音。