【问题标题】:How do you add background music to SpriteKit?如何将背景音乐添加到 SpriteKit?
【发布时间】:2015-11-01 19:34:49
【问题描述】:

我试过这样做:

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.

提前谢谢你!

【问题讨论】:

    标签: ios swift sprite-kit swift2 background-music


    【解决方案1】:

    (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 声明为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
          }
      }
      

      【讨论】:

      • 我应该把课程和try-catch放在哪里?
      • 嗯...现在我收到此错误:fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)soundFilePath!nil
      猜你喜欢
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多