Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 场景更改后音乐停止播放SKAudioNode Xcode 8_Ios_Xcode_Sprite Kit - Fatal编程技术网

Ios 场景更改后音乐停止播放SKAudioNode Xcode 8

Ios 场景更改后音乐停止播放SKAudioNode Xcode 8,ios,xcode,sprite-kit,Ios,Xcode,Sprite Kit,所以我一直在尝试让这个音频文件在游戏的所有场景中播放,但它在MenuScene之后被切断了。我从GameViewController class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let view = self.view as! SKView? { // Load the SKScene from 'MenuSc

所以我一直在尝试让这个音频文件在游戏的所有场景中播放,但它在MenuScene之后被切断了。我从
GameViewController

class GameViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'MenuScene.sks'
        if let scene = SKScene(fileNamed: "MenuScene") {
            // Set the scale mode to scale to fit the window
然后在MenuScene中,我通过这样做开始音乐

class MenuScene: SKScene {

var starfield:SKEmitterNode!

var newGameButtonNode:SKSpriteNode!
var difficultyButtonNode:SKSpriteNode!
var difficultyLabelNode:SKLabelNode!
var backgroundMusic:SKAudioNode!

override func didMove(to view: SKView) {


    let backgroundMusic = SKAudioNode(fileNamed: "Snap.mp3")
    backgroundMusic.autoplayLooped = true
    addChild(backgroundMusic)
音乐开始播放。然后我在
触摸开始()
中,如果触摸游戏场景按钮,它将加载该场景。我做了很长时间的过渡,以确定音乐何时停止并在整个过渡过程中播放,但当
游戏场景
加载时,它会停止

if nodesArray.first?.name == "newGameButton" {

            let transition = SKTransition.fade(withDuration: 5.0)

            run(SKAction.wait(forDuration: 5.0), completion: {
                self.backgroundMusic = SKAudioNode(fileNamed: "Snap.mp3")
                self.backgroundMusic.autoplayLooped = true
                self.addChild(self.backgroundMusic)
            })
            let gameScene = GameScene(size: self.size)

            self.view!.presentScene(gameScene, transition: transition)

我希望它能在整个游戏和游戏场景中继续播放,并永远循环播放

确保在主场景和

您可以这样做:

self.runAction(SKAction.playSoundFileNamed("YourFileName.extension", waitForCompletion: false))
重复音乐:

SKAction.repeatForever(SKAction.playSoundFileNamed("YourFileName.extension", waitForCompletion: true))
你的音乐将继续播放

与AVF基金会合作

我的一段代码我已经完成了

import AVFoundation


var backgroundMusicPlayer: AVAudioPlayer!

func playBackgroundMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(
filename, withExtension: nil)
if (url == nil) {
print("Could not find file: \(filename)")
return
}

var error: NSError? = nil
backgroundMusicPlayer = 
AVAudioPlayer(contentsOfURL: url, error: &error)
if backgroundMusicPlayer == nil {
print("Could not create audio player: \(error!)")
return
}

backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
 }

  import SpriteKit

我怎么才能让它重复呢,我试过了,每个场景都会播放,但只有一次。我确实把你加的台词放在了它下面,那首歌没有重复。我觉得这是因为我的MenuScene只在游戏开始时被调用,但当按下相关按钮时,GameSecene和GameOverScene会互相调用。如何让它在MenuScene中播放并在GameSecene和GameOverScene中重复?尝试将第一行代码放入MenuScene,然后在GameSecene和GameOverScene中放入第二行代码。如果有效的话,请告诉我。它仍然不会重复。我会接受你的回答。我做了一些稍微不同的事情来解决我的问题,但你的代码确实帮助我找到了解决问题的方法。我能在你的回答中加上我所做的帮助我弄明白的事情吗?