如何在SpriteKit中添加背景音乐?

4
我尝试了这个:
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个回答

7

(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使这变得非常简单。希望这能帮到你。


2
在Swift 2中,类型为NSErrorPointer的参数被省略了,可在catch块中捕获,如下所示:
do 
{
    let player = try AVAudioPlayer(contentsOfURL: soundFileURL, fileTypeHint: nil)
} 
catch let error as NSError
{
    print(error.description)
}

补充说明:

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

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
    }
}

我应该把类和 try-catch 放在哪里? - Raxzon
嗯...现在我遇到了这个错误:fatal error: unexpectedly found nil while unwrapping an Optional value (lldb),它说 soundFilePath!nil - Raxzon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接