SKAction playSoundFileNamed停止背景音乐 [英] SKAction playSoundFileNamed stops background music

查看:324
本文介绍了SKAction playSoundFileNamed停止背景音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want my SpriteKit game not to interrupt background music that user listens (Music.app or radio 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?

推荐答案

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

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.

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

For me my solution included two parts. First, I had to initialize my SoundAction SKAction within my didMoveToView function in my scene file. 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)
    }

}

然后在AppDelegate文件中,您需要在应用程序的didFinishLaunchingWithOptions函数中放置以下代码行。 (不要忘记在您的APPDELEGATE中导入反对意见)。所以你的app委托方法应该是这样的:

Then in your AppDelegate file you need to place the following line of code in your application didFinishLaunchingWithOptions function. (DO NOT FORGET TO IMPORT AVFOUNDATION IN YOUR APPDELEGATE). So your app delegate method should like something like this:

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.

这篇关于SKAction playSoundFileNamed停止背景音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆