gpt4 book ai didi

ios - 由于 sprite-kit 游戏中未完成的声音,我的应用程序崩溃了

转载 作者:可可西里 更新时间:2023-11-01 02:05:57 24 4
gpt4 key购买 nike

无法理解为什么我的应用程序在播放声音时总是崩溃并进入后台。如果这里没有声音,应用程序不会崩溃。我有一个播放声音的 SKAction。我在 Actions 类中使用 static let 预加载这首歌曲,并调用 static func 返回带声音的 SKAction。所以我有完成级别的声音。当它播放时,我按下主页按钮,他们回到应用程序。应用程序崩溃并显示消息“Thread 1: EXC_BAD_ACCESS(code=1, address=0xff8e9abc)”。有人知道这里发生了什么吗?

这是我的名为 Actions 的结构:

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

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

在我的类 Grid 中,我有关卡是否完成的条件:

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

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

gameNode 中,我有 grid 实例,这是最后一个的委托(delegate),我将 completionNode 添加到场景中:

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

当我到达关卡的尽头时,我按下主页按钮,然后在发生崩溃后返回我的游戏。

最佳答案

当您的应用/游戏在前台和后台模式之间转换时,playSoundFileNamed 似乎无法保留 Audio Session 。

根据您的实现,应用程序在中断后恢复后会出现不可预知的行为,从静音播放到加载声音 Assets 失败再到完全崩溃,就像您的情况一样。

这里有个讨论类似问题的话题:SKAction playSoundFileNamed doesn't work after receiving two consecutive phone calls

我会建议停止使用 playSoundFileNamed 操作并改用 AVAudioPlayer 来播放声音文件。配置它需要更多的努力,但它已经存在了很长时间并且运行完美。

关于ios - 由于 sprite-kit 游戏中未完成的声音,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42874601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com