0

我有一个功能可以播放在一个视图控制器中完美运行的音乐。我将相同的代码复制到一个新的视图控制器中,现在遇到以下错误:“在展开可选值时意外发现 nil”,代码部分被星号包围。

  func playMe(theSong: String) {
    **let url = URL.init(fileURLWithPath: Bundle.main.path(forResource: theSong, ofType: "mp3")!)**
    do {
        try jukeBox = AVAudioPlayer(contentsOf: url)
        jukeBox?.delegate = self
        jukeBox?.prepareToPlay()
    } catch let error as NSError {
        print("audioPlayer error \(error.localizedDescription)")
    }

    // this line tells the jukebox to play!
    if let player = jukeBox {
        player.play()
    }
4

0 回答 0