Как играть рингтон без перерыва, Swift - PullRequest
0 голосов
/ 09 декабря 2018

Я пытаюсь воспроизводить и зацикливать рингтон при нажатии кнопки, при воспроизведении рингтона у меня также работает другая логика.

Рингтон воспроизводится без прерываний при тестировании без другой логики, но прерывается приЯ добавляю другую логику.

Как мне избежать этого прерывания?Примечание. Мелодия воспроизводится в течение секунды или двух, а затем останавливается.

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")
        }
    }

}
...