我的应用程序由于Sprite-kit游戏中未完成的声音而崩溃 [英] My app crashes because of unfinished sounds in my sprite-kit game

查看:37
本文介绍了我的应用程序由于Sprite-kit游戏中未完成的声音而崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法理解为什么我的应用在播放声音时总是在后台崩溃.如果这里没有声音,应用程序将不会崩溃.我有一个播放声音的 SKAction .我在 Actions 类中通过 static let 预加载了这首歌曲,并调用了 static func ,该函数返回了带有声音的 SKAction .所以我有完成水平的声音.播放时,我按了主页按钮,他们又回到了应用程序.应用程序崩溃并显示消息线程1:EXC_BAD_ACCESS(code = 1,adress = 0xff8e9abc)".有人知道这里发生了什么吗?

Can't understand why my app crashes always going background when a sound is playing. If no sound here app won't crash. I have a SKAction which plays a sound. I preload this songs with static let in Actions class and call static func which returns SKAction with sound. So I have completion level sound. When it plays, I hit home button, them come back to app. There app crashes with message "Thread 1: EXC_BAD_ACCESS(code=1, adress=0xff8e9abc)". Has anybody an idea what's going on here?

这是我的名为 Actions 的结构:

public struct Actions {
    static let completionSound = SKAction.playSoundFileNamed("CompletionSound.mp3", waitForCompletion: false)

    static func playCompletionSound() -> SKAction {
        return completionSound
    }
}

在我的名为 Grid 的课程中,我有条件确定是否完成级别:

Here in my class called Grid, I have condition whether level completed or not:

class Grid {
    var delegate: GridDelegate?
    private var completed: Bool = false
    ...

    ...
    private func completeLevel() {
        if completed {
            let waitAction = SKAction.wait(forDuration: 1.5)
            let blockAction = SKAction.run {
                let groupAction = self.settings.sound ? SKAction.group([Actions.playGridSwishSound(), Actions.removeGridAction()]) : Actions.removeGridAction()
                self.gridNode.run(groupAction, completion: {
                    self.gridNode.removeAllChildren()
                    self.delegate?.completionLevel()
                })
            }
            let seq = SKAction.sequence([waitAction, blockAction])
            run(groupAction)
        }
    }

我有一个节点,其中显示带有文本的 SKLabelNode .

There I have a node, which shows SKLabelNode with "COMPLETED" text.

class CompletionNode: SKNode {
    private let completionLabel = SKLabelNode(fontNamed: MainFont)

    init(settings: Settings) {
        self.settings = settings
        super.init()
        isUserInteractionEnabled = false

        completionLabel.text = Titles.CompletedTitle
        completionLabel.fontSize = 46.0
        completionLabel.fontColor = GoldenColor
        completionLabel.centering()
        completionLabel.position = CGPoint(x: 0.0, y: 0.0)
        completionLabel.xScale = 0.0
        completionLabel.yScale = 0.0
        completionLabel.alpha = 0.0

        addChild(completionLabel)
    }

    func animate(completion: @escaping () -> ()) {
        let waitAction = Actions.waitAction(duration: 0.2)
        let scaleAction = SKAction.scale(to: 1.0, duration: 0.4)
        scaleAction.timingMode = .easeInEaseOut
        let fadeInAction = Actions.fadeInAction(duration: 0.4)
        fadeInAction.timingMode = .easeIn
        let groupAction = SKAction.group([scaleAction, fadeInAction])

        let llBlock = SKAction.run {
            self.completionLabel.run(groupAction)
        }

        let seq = SKAction.sequence([waitAction, llBlock])
        run(seq, completion: completion)
    }
}

在我有 grid 实例的 gameNode 中,该实例是最后一个实例的代表,我在场景中添加了completionNode:

In gameNode where I have grid instance, which is the delegate of last one, I add completionNode to the scene:

class GameScene: SKNode {
    var gird: Grid?
    let hudNode = HUDNode()
    let completionNode = CompletionNode()
    var completionOn = false
    ...
    init() {
        super.init()
        grid = Grid()
        grid!.delegate = self
        grid!.position = CGPoint(x: 0.0, y: 0.0)
        addChild(grid!)

        hudNode.position = CGPoint(x: 0.0, y: MainSize.height * 0.44)
        hudNode.zPosition = 40.0
        hudNode.delegate = self
        addChild(hudNode)

        completionNode.alpha = 0.0
        completionNode.position = CGPoint(x: 0.0, y: 0.0)
        completionNode.zPosition = 60.0
        addChild(completionNode)
    }

    internal func completeLevel() {
        addCompletionScene()
    }

    private func addCompletionNode() {
        run(Actions.playCompletionSound())
        completionNode.alpha = 1.0
        completionNode.animate {
            self.completionOn = true
            self.hudNode.isUserInteractionEnabled = true
        }
    }
}

当我到达关卡末尾时,按主页按钮,然后在发生崩溃后返回游戏.

When I reach the end of level I press home button, then come back to my game, after what crash happens.

推荐答案

当您的应用程序/游戏在前景和背景模式之间转换时,playSoundFileNamed似乎在保存音频会话时遇到问题.

It appears playSoundFileNamed has problems preserving the audio session while your app/game transitions between foreground and background mode.

根据您的实现,在中断后应用程序恢复运行后,您会遇到无法预料的行为,从静音播放到加载声音资产失败到像您遇到的普通崩溃一样.

Depending on your implementation, after the app resumes after the interruption you get unpredictable behavior ranging from muted playback to failed loading sound assets to plain crashing as in your case.

这里是讨论类似问题的主题:收到两个连续的电话后,SKAction playSoundFileNamed无法正常工作

Here is a topic that discusses similar problems: SKAction playSoundFileNamed doesn't work after receiving two consecutive phone calls

我建议停止使用playSoundFileNamed动作,而改用AVAudioPlayer播放声音文件.配置它需要花费更多的精力,但是它已经存在了很长时间并且可以完美地工作.

I would suggest to stop using playSoundFileNamed actions and use AVAudioPlayer for playing sound files instead. It takes more effort into configuring it but it has been around for a long time and works flawlessly.

这篇关于我的应用程序由于Sprite-kit游戏中未完成的声音而崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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