繁体   English   中英

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

[英]How do you add background music to SpriteKit?

我尝试这样做:

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

先感谢您!

(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 让这一切变得非常简单。 我希望这可以帮助你。

在 Swift 2 中, NSErrorPointer类型的参数被省略了,可以在catch块中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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM