由于 SimpleAudioEngine,应用程序在后台崩溃
因此,我尝试使用 SimpleAudioEngine 在我的 Cocos2D 游戏中播放一些效果,但在添加它们后,我的应用程序在进入后台(多任务)时崩溃。 我在互联网上搜索了这个问题,但我找到的所有解决方案都不适合我。我发现这个问题的发生是因为我的应用程序在后台尝试播放声音。
在控制台中它向我显示(这与我发现其他人遇到的错误相同):
sgx 错误(不允许后台 GPU 访问):
另一件事是,当我在模拟器上运行我的应用程序时,甚至在我的设备上仔细调试时(在应用程序运行时使用 XCode 逐行运行),这不会发生。
So I׳m trying to play some effects in my Cocos2D game using SimpleAudioEngine , but after I have added them my app crashes when it goes to background (multitasked).
I searched for this problem in the internet but all the solutions that I found didn't work for me. What I did find out is that this problem happens because my app is somehow trying to play sounds when backgrounded.
In console it shows me (which is the same error I found other people had):
sgx error (background gpu access not permitted):
And another thing, when I run my app on the simulator, or even on my device while debugging carefully (going line-by-line with XCode while the app is running) this doesn't happen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚遇到这个问题。我通过使用一个布尔值来检查应用程序是否正在运行或在后台来解决这个问题,当应用程序转到前台时( applicationWillEnterForeground )我将其设置为 true ,当应用程序转到后台时( applicationDidEnterbackground )我将其设置为 false 。因此,使用 bool 可以判断应用程序是否在后台,如果是,我只需退出 EAGLView 中的 drawView 函数(因此不会执行任何导致错误的图形渲染)。
我是一个非常躲闪的程序员,但这种方法对我有用,我希望它对其他人也有用。我不需要卸载并重新加载我的声音或任何东西,我的应用程序现在具有多任务处理功能 XD
I just had this issue. I resolved it by having a bool to check if the app is running or in background that I set to true when the app goes to foreground ( applicationWillEnterForeground ) and that I set to false when the app goes to background ( applicationDidEnterbackground ) . So using the bool you can tell if the app is in the background and if it is, I just exit out of drawView function in EAGLView (thus not doing any graphics rendering which was causing the error).
I am a very dodge programmer but that method has worked for me and I hope it works for someone else. I did not need to unload and reload my sounds or anything and my app now has Multitasking XD
我在大约 25% 的应用程序重新进入前台的情况下遇到过这种情况。和你一样,当我删除声音后,问题就消失了。这就是我在这里遇到你的问题的方式。
我可能已经找到了解决这个问题的方法。我做了一些看似无关的更改,但问题似乎已经消失了。现在,当我的应用程序进入后台时,我将使主计划计时器无效。当我的应用程序重新进入前台时,我重新安排计时器(在重新加载声音后,我在进入后台时完全关闭了计时器)。
到目前为止,问题还没有回来。我很想知道这是否有帮助。
I was experiencing this, on about 25% of the occasions that my application re-entered the foreground. Like you, when I removed the sounds, the problem went away. That is how I came across your question here.
I may have found a solution to this. I have made what appears to be an unrelated change, but the problem seems to have gone away. Now, when my app enters the background, I invalidate my main scheduled timer. When my app re-enters the foreground, I then re-schedule the timer (after reloading my sounds, which I completely shut down on entering background).
So far, the problem has not come back. I would be interested to know if this helps.
我刚刚解决了这个问题。这是我的案例中的错误,从本页上的其他答案和评论中我可以看出,许多其他人的案例也是如此:
默认情况下,当我开始我的项目时,CCDirector::sharedDirector()- >pause(); 和 CCDirector::sharedDirector()->resume(); 都被调用了两次,一次是由 (void)applicationWillResignActive:(UIApplication * ) 和 (void)applicationDidBecomeActive:(UIApplication *) 分别在 AppController.mm 中,并通过 AppDelegate::applicationDidEnterBackground() 一次> 和 AppDelegate::applicationWillEnterForeground() 分别位于 AppDelegate.cpp 中。
绝对确保这些方法仅在 AppController.mm 中调用一次。在 AppDelegate.cpp 中,请确保您调用的是 CCDirector::sharedDirector()->stopAnimation(); 来代替 CCDirector::sharedDirector()->pause() ; 和 CCDirector::sharedDirector()->startAnimation(); 代替 CCDirector::sharedDirector()->resume();。
希望这对陷入这种糟糕情况的其他人有帮助!
I just resolved this issue on my end. Here's what was wrong in my case and, from what I can tell from the other answers and comments on this page, many other people's case as well:
By default, when I started my project, CCDirector::sharedDirector()->pause(); and CCDirector::sharedDirector()->resume(); were both being called twice, once by (void)applicationWillResignActive:(UIApplication *) and (void)applicationDidBecomeActive:(UIApplication *) respectively in AppController.mm, and once by AppDelegate::applicationDidEnterBackground() and AppDelegate::applicationWillEnterForeground() respectively in AppDelegate.cpp.
Make absolutely sure that these methods are only being called once, in AppController.mm. In AppDelegate.cpp, instead make sure that you are calling CCDirector::sharedDirector()->stopAnimation(); in place of CCDirector::sharedDirector()->pause(); and CCDirector::sharedDirector()->startAnimation(); in place of CCDirector::sharedDirector()->resume();.
Hope that's helpful to anyone else stuck in this crappy situation!
你确定这和音频有关吗? “后台 GPU 访问”听起来像是使用 OpenGL。
Are you sure it's related to audio? "background gpu access" sounds like it's using OpenGL.
我的申请中也遇到了同样的问题,花了大约 4 个小时才找到答案。第一次进入后台正常,但第二次应用程序崩溃。带有与 OpenGL 相关的简短错误消息。我有同样的问题:音频如何使图形崩溃。但这不是音频问题,而是通知问题...
我发现进入前台是在我的自定义电平表类中创建 2 个计时器。
我注册了 UIApplicationWillEnterForegroundNotification 和 UIApplicationWillResignActiveNotification。然后,进入后台只会使一个无效,因为我只是根据通知注册的......就是这样!
一个需要数它的通知!
I had the same issue in my application and spent some 4 hours to find out. Going background was OK the first time, but crash application the second time. With a short error message related to OpenGL. I had the same questions: how audio can crash graphics. But it wasn't a question of audio, but a question of notifications...
I discovered that going foreground was creating 2 timers in my custom level meter class.
I had registered UIApplicationWillEnterForegroundNotification and UIApplicationWillResignActiveNotification. Then, going background invalidated only one, since I registered only on notification... That was it!
One need's count its notifications!