简体   繁体   English

SKAction playSoundFileNamed会停止背景音乐

[英]SKAction playSoundFileNamed stops background music

I want my SpriteKit game not to interrupt background music that user listens (Music.app or radio app). 我希望我的SpriteKit游戏不会中断用户收听的背景音乐(Music.app或无线电应用程序)。

Everything goes fine until execution reaches this line: 一切顺利,直到执行到达这一行:

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

After this line background music stops. 此线背景音乐停止后。 How to avoid this? 怎么避免这个?

I have also encountered this problem myself. 我自己也遇到过这个问题。 FYI my solutions are in swift so just translate this to Objective C if you are still using that. 仅供参考我的解决方案是快速的,所以如果您还在使用它,请将其转换为Objective C.

For me my solution included two parts. 对我来说,我的解决方案包括两部分 First, I had to initialize my SoundAction SKAction within my didMoveToView function in my scene file. 首先,我必须在我的场景文件中的didMoveToView函数中初始化我的SoundAction SKAction。 So your scene file should look something like this: 所以你的场景文件应该是这样的:

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)
    }

}

Then in your AppDelegate file you need to place the following line of code in your application didFinishLaunchingWithOptions function. 然后在AppDelegate文件中,您需要在应用程序的didFinishLaunchingWithOptions函数中放置以下代码行。 (DO NOT FORGET TO IMPORT AVFOUNDATION IN YOUR APPDELEGATE). (不要忘记在您的APPDELEGATE中导入反对意见)。 So your app delegate method should like something like this: 所以你的app delegate方法应该是这样的:

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
}

Hope this solves it for you as it did for me! 希望这能为你解决它,就像它对我一样! Let me know if it doesn't. 如果没有,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM