Я использую AVAudioPlayer для проигрывания аудио, но вылетает ошибка.
Вот мой код:
var audioPlayer:AVAudioPlayer!
var soundChange = true
@IBAction func play(_ sender: Any) {
let url = NSURL(string: "/upload/iblock/f64/f64aa1d14b7fb2fe5de5f68922ee506f.mp3")! as URL
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateAudioProgressView), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: false)
} catch {
print("An error occurred while trying to extract audio file")
}
Тут выводит ошибку: unexpectedly found nil while unwrapping an Optional value
if !audioPlayer.isPlaying && soundChange == true{
playChange.setImage(UIImage(named:"Pause.png"), for: UIControlState.normal)
soundChange = false
audioPlayer.play()
} else {
playChange.setImage(UIImage(named:"Play.png"), for: UIControlState.normal)
soundChange = true
audioPlayer.pause()
}
}