When I am trying to run my code, I get the error in the title above in my code on the line that says

audioPlayer = try AVAudioPlayer(contentsOf: url)

This code worked like a charm before I updated my Xcode into Xcode 11, and now with swift 5, I had to change it and now it doesn't work. can someone please help me? Here is my code

import UIKit
import AVFoundation

class ViewController: UIViewController, AVAudioPlayerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    playAudio()

}

@IBAction func playButton(_ sender: Any) {
playAudio()
}


var indexPath = 0
var listOfTheSongs = ["badLiar.mp3", "cool.mp3", "eastSide.mp3", "fallinAllInYou.mp3", "hateHowMuchILoveYou.mp3", "inMyBlood.mp3", "letMeDownSlowly.mp3", "loveYourself.mp3", "panini.mp3", "rewriteTheStars.mp3", "stiches.mp3", "treatYouBetter.mp3", "wow.mp3"]
 var audioPlayer = AVAudioPlayer()
 var path = Bundle.main.path(forResource: "badLiar.mp3" , ofType: nil , inDirectory: "Songs For Lyric Hunt")!


func playAudio () {

    if path != nil {
        print("path does not equal nil")
        let url = URL(fileURLWithPath: path)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            if audioPlayer.isPlaying == true {
            audioPlayer.stop()
            } else {
                audioPlayer.play()
            }
        } catch {
            // couldn't load file :(
            print("counldn't load file :(")
        }
    }else {
        print(" path equals to nil")

        }
    }



}

Related posts

Recent Viewed