Rax*_*zon 4 background-music ios sprite-kit swift swift2
我尝试这样做:
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()
Run Code Online (Sandbox Code Playgroud)
我把这段代码放在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)
}
Run Code Online (Sandbox Code Playgroud)
如果您想在某个时候停止背景音乐,您可以使用内置方法:
backgroundSound.run(SKAction.stop())
Run Code Online (Sandbox Code Playgroud)
或者,您可能想在停止后再次播放声音:
backgroundSound.run(SKAction.play())
Run Code Online (Sandbox Code Playgroud)
SpriteKit 让这一切变得非常简单。我希望这可以帮助你。
| 归档时间: |
|
| 查看次数: |
3171 次 |
| 最近记录: |