我有一个功能来播放在一个视图控制器中完美运行的音乐。我将相同的代码复制到一个新的视图控制器中,现在遇到以下错误:“在解包一个Optional值时意外发现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()
}