当按下按钮时,我正在尝试播放和循环铃声,在播放铃声时我还有其他逻辑运行 .

在没有其他逻辑的情况下进行测试时,铃声不会中断,但在添加其他逻辑时会被中断 .

我怎样才能避免这种中断?注意:铃声播放一两秒然后停止 .

func makeVideoCall(callReceiverId : String) {

        self.playSound(nameOfAudioFileInAssetCatalog: "outgoingRingtone2")

        // Other logic/code executed here, like making calls to the backend and calling callKit functions

}


func playSound(nameOfAudioFileInAssetCatalog: String) {
    //private

    if let sound = NSDataAsset(name: nameOfAudioFileInAssetCatalog) {
        do {
            try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
            try! AVAudioSession.sharedInstance().setActive(true)
            try self.soundPlayer = AVAudioPlayer(data: sound.data, fileTypeHint: AVFileTypeWAVE)
            self.soundPlayer!.numberOfLoops = -1 // By setting to -1 the sound will be played infinitely, and will be stopped when the participant adds videotrack or when there is an error connecting the call
            self.soundPlayer!.play()
        } catch {
            print("error initializing AVAudioPlayer")
        }
    }

}