简体   繁体   English

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

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

I tried doing this:我尝试这样做:

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()

I put this code in the didMoveToView function, and I have import AVFoundation at the top of my code.我将此代码放在didMoveToView函数中,并且在代码顶部import AVFoundation I get the error: Call can throw, but it is not marked with 'try' and the error is not handled .我收到错误: Call can throw, but it is not marked with 'try' and the error is not handled

Thank you in advance!先感谢您!

(Xcode 8 and Swift 3) (Xcode 8 和 Swift 3)

Using SpriteKit this is pretty simple.使用 SpriteKit 这非常简单。 You create an SKAudioNote with a sound file and then you attach it to your Scene as a child.您创建一个带有声音文件的 SKAudioNote,然后作为孩子将其附加到您的场景中。 It will loop by default:默认情况下它会循环:

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

If you want to stop the background music at some point you can use the build in methods:如果您想在某个时候停止背景音乐,您可以使用内置方法:

    backgroundSound.run(SKAction.stop())

Or maybe you want to play the sound again after stopping it:或者,您可能想在停止后再次播放声音:

    backgroundSound.run(SKAction.play())

SpriteKit makes this really simple. SpriteKit 让这一切变得非常简单。 I hope this helps you.我希望这可以帮助你。

In Swift 2, the parameter of type NSErrorPointer is left out and can be caught within catch block as follows:在 Swift 2 中, NSErrorPointer类型的参数被省略了,可以在catch块中catch ,如下所示:

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

Addendum:附录:

It is better to declare your AVAudioPlayer as an ivar;最好将您的AVAudioPlayer声明为 ivar; otherwise, it could be released and thus the music won't play:否则,它可能会被释放,因此音乐不会播放:

class yourViewController: UIViewController 
{
    var player : AVAudioPlayer!

....

So, given that, we make a minor change to the aforementioned try-catch :因此,鉴于此,我们对上述try-catch进行了微小更改:

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

Edit:编辑:

What you originally have:你原来拥有的:

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()

Hereby adding the catch clause in Swift 2:特此在 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()

Assuming your class is a subclass of SKScene :假设您的类是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