首先,我要事先感谢任何人得到的任何帮助。
我已经在整个网中搜索了很远,找不到解决我问题的解决方案。我的问题是使用SpriteKit框架建造的iOS游戏。我已经使用skaudionode添加了背景歌曲,最初效果很好,但是当我停下来玩游戏并在几秒钟内玩游戏时,音乐不会再开始播放。我尝试了很多事情,例如暂停游戏时删除Skaudionode并在恢复游戏时再次添加它,但没有任何效果。我在下面发布了我的代码的片段,使其尽可能相关:
class GameScene: SKScene, SKPhysicsContactDelegate {
var backgroundMusic = SKAudioNode(fileNamed: "bg.mp3")
let pauseImage = SKTexture(imageNamed: "pause.png")
var pauseButton:SKSpriteNode!
let playImage = SKTexture(imageNamed: "play2.png")
var playButton:SKSpriteNode!
override func didMoveToView(view: SKView) {
self.addChild(backgroundMusic)
// create pause button
pauseButton = SKSpriteNode(texture: pauseImage)
pauseButton.position = CGPoint(x: self.size.width - pauseButton.size.width, y: pauseButton.size.height)
pauseButton.zPosition = 1
pauseButton.name = "pauseButton"
self.addChild(pauseButton)
// create play button
playButton = SKSpriteNode(texture: playImage)
playButton.position = CGPoint(x: self.size.width - playButton.size.width, y: -playButton.size.height)
playButton.zPosition = 1
playButton.name = "playButton"
self.addChild(playButton)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if pauseButton.containsPoint(touch.locationInNode(self)) {
let blockOne = SKAction.runBlock({
self.pauseButton.position.y = - self.pauseButton.size.height
self.playButton.position.y = self.playButton.size.height
})
let blockTwo = SKAction.runBlock({
self.view?.paused = true
})
self.runAction(SKAction.sequence([blockOne, blockTwo]))
}
else if(playButton.containsPoint(touch.locationInNode(self))) {
self.playButton.position.y = -self.playButton.size.height
self.pauseButton.position.y = self.pauseButton.size.height
self.view?.paused = false
}
}
}
}
您需要考虑使用Avaudioplayer和NSnotificationCenter在游戏周围传递数据。
在实际的GameViewController类中启动背景音频播放器。最好这样做,然后使用Skaudionode ...这更多的是与游戏玩法中发生的事情相关的声音。
通过使用avaudioplayer,优势是当音乐暂停时,它仍然可以在以前的位置播放。
这是不管发生了什么的几件事之一。因此,我们将其放在GameViewController中。
所以这是我们需要启动
的GameViewController代码的示例import AVFoundation
var bgMusicPlayer:AVAudioPlayer?
然后在GameViewController中,我们将这些功能作为
override func viewDidLoad() {
super.viewDidLoad()
// PlayBackgroundSound , PauseBackgroundSound will be your code to send from other "scenes" to use the audio player //
// NSNotificationCenter to pass data throughout the game //
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.playBackgroundSound(_:)), name: "PlayBackgroundSound", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.pauseBackgroundSound), name: "PauseBackgroundSound", object: nil)
}
func playBackgroundSound(notification: NSNotification) {
let name = notification.userInfo!["fileToPlay"] as! String
if (bgSoundPlayer != nil){
bgSoundPlayer!.stop()
bgSoundPlayer = nil
}
if (name != ""){
let fileURL:NSURL = NSBundle.mainBundle().URLForResource(name, withExtension: "mp3")!
do {
bgSoundPlayer = try AVAudioPlayer(contentsOfURL: fileURL)
} catch _{
bgSoundPlayer = nil
}
bgSoundPlayer!.volume = 1
bgSoundPlayer!.numberOfLoops = -1
// -1 will loop it forever //
bgSoundPlayer!.prepareToPlay()
bgSoundPlayer!.play()
}
}
func pauseBackgroundSound() {
if (bgSoundPlayer != nil){
bgSoundPlayer!.pause()
}
}
然后,当您想在暂停或恢复按钮功能中使用音频播放器时。
请记住,您需要在每个场景中使用玩家。
import AVFoundation.AVAudioSession
override func didMoveToView(view: SKView) {
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
}
然后,如果您想暂停或玩某些东西,只需使用NSNOTIFICYCENTER。
//在场景中暂停使用此函数中的代码行,您需要暂停音乐
NSNotificationCenter.defaultCenter().postNotificationName("PauseBackgroundSound", object: self)
///最初播放或一首新歌曲。在您需要播放音乐的功能中使用此代码
NSNotificationCenter.defaultCenter().postNotificationName("PlayBackgroundSound", object: self, userInfo: "FILE NAME OF BACKGROUND MUSIC")