你如何向SpriteKit添加背景音乐?

时间:2015-08-10 00:44:15

标签: ios sprite-kit swift2 background-music

我试过这样做:

let soundFilePath = NSBundle.mainBundle().pathForResource("GL006_SNES_Victory_loop.aif", ofType: "GL006_SNES_Victory_loop.aif")
    let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
    let player = AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
    player.numberOfLoops = -1 //infinite
    player.play()

我将此代码放在didMoveToView函数中,我的代码顶部有import AVFoundation。我收到错误: Call can throw, but it is not marked with 'try' and the error is not handled

提前谢谢!

2 个答案:

答案 0 :(得分:3)

(Xcode 8和Swift 3)

使用SpriteKit非常简单。您使用声音文件创建SKAudioNote,然后将其作为孩子附加到场景中。它将默认循环:

override func didMove(to view: SKView) {
    let backgroundSound = SKAudioNode(fileNamed: "bg.mp3")
    self.addChild(backgroundSound)
}

如果您想在某些时候停止背景音乐,可以使用内置方法:

    backgroundSound.run(SKAction.stop())

或许你想在停止后再次播放声音:

    backgroundSound.run(SKAction.play())

SpriteKit让这很简单。我希望这会对你有所帮助。

答案 1 :(得分:2)

在Swift 2中,类型NSErrorPointer的参数被省略,可以在catch块中捕获,如下所示:

do 
{
    let player = try AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
} 
catch let error as NSError
{
    print(error.description)
}

附录:

最好将您的AVAudioPlayer声明为ivar;否则,它可能被释放,因此音乐将无法播放:

class yourViewController: UIViewController 
{
    var player : AVAudioPlayer!

....

因此,鉴于此,我们对前面提到的try-catch进行了一些小改动:

do 
{
    player = try AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
} 
catch let error as NSError
{
    print(error.description)
}

<强> 编辑:

你最初拥有的东西:

let soundFilePath = NSBundle.mainBundle().pathForResource("GL006_SNES_Victory_loop.aif", ofType: "GL006_SNES_Victory_loop.aif")
let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
let player = AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
player.numberOfLoops = -1 //infinite
player.play()

特此在Swift 2中添加catch子句:

let soundFilePath = NSBundle.mainBundle().pathForResource("GL006_SNES_Victory_loop.aif", ofType: "GL006_SNES_Victory_loop.aif")
let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
do 
{
    player = try AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
} 
catch let error as NSError
{
    print(error.description)
}
player.numberOfLoops = -1 //infinite
player.play()

假设您的类是SKScene的子类:

class yourGameScene: SKScene
{
    //declare your AVAudioPlayer ivar
    var player : AVAudioPlayer!

    //Here is your didMoveToView function
    override func didMoveToView(view: SKView)
    {
         let soundFilePath = NSBundle.mainBundle().pathForResource("GL006_SNES_Victory_loop.aif", ofType: "GL006_SNES_Victory_loop.aif")
         let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
         do 
         {
             player = try AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
         } 
         catch let error as NSError
         {
             print(error.description)
         }
         player.numberOfLoops = -1 //infinite
         player.play()

         //The rest of your other codes
    }
}
相关问题