2014-02-17 134 views
2

我希望我的SpriteKit游戏不会中断用户收听的背景音乐(Music.app或广播应用程序)。SKAction playSoundFileNamed停止背景音乐

sSharedShootSoundAction = [SKAction playSoundFileNamed:@"plane_shoot.aiff" 
            waitForCompletion:NO]; 

此行的背景音乐停止后:直到执行到达此行

一切顺利。如何避免这种情况?

+0

是否预装了该音频文件? – prototypical

+0

不,这不是... –

+0

尝试预加载它... :) – prototypical

回答

6

我自己也遇到过这个问题。仅供参考,我的解决方案很快,所以如果您仍在使用它,请将其转换为Objective C。

对我来说,我的解决方案包括两个部分。首先,我必须在场景文件的didMoveToView函数中初始化我的SoundAction SKAction。所以,你的场景文件应该是这个样子:

import SpriteKit 
import GameKit 
import AVFoundation 

class GameScene: SKScene, SKPhysicsContactDelegate { 

    var sSharedShootSoundAction: SKAction = SKAction() 

    override func didMoveToView(view: SKView) { 
     /* Setup your scene here */ 
     sSharedShootSoundAction = SKAction.playSoundFileNamed("plane_shoot.aiff", waitForCompletion: false) 
    } 

} 

然后在您的AppDelegate文件,你需要将下面的代码行中的应用程序didFinishLaunchingWithOptions功能。 (请勿忘记导入您的APPDELEGATE AVFOUNDATION)。所以你的应用代理方法应该像这样:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 

    // INSERT THIS LINE BELOW. 
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient, error: nil) 

    return true 
} 

希望这能为你解决它,就像它为我做的那样!让我知道如果它不。