我在尝试为游戏添加声音时遇到了问题。我将以下内容添加到视图控制器中,当我构建并运行时,我会收到错误。当我运行以下代码时:
var interstitialAd : GADInterstitial!
var player: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "gameMusic.mp3", ofType:"mp3")!
let url = URL(fileURLWithPath: path)
do {
let sound = try AVAudioPlayer(contentsOf: url)
player = sound
sound.play()
} catch {
print("file not found")
}
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
我收到以下错误:
致命错误:在展开Optional值时意外发现nil 2017-05-21 17:35:01.262683 gameTest2 [1660:363851]致命错误: 在展开可选值时意外发现nil
有人可以帮忙吗?
答案 0 :(得分:0)
当您使用Bundle.main.path(forResource:,ofType:)
时,第一部分应该是资源的名称,第二部分应该是其类型。这意味着您的代码应该是:
let path = Bundle.main.path(forResource: "gameMusic", ofType:"mp3")!