简体   繁体   English

由于SimpleAudioEngine,应用程序因背景而崩溃

[英]App crashes on backgrounding because of SimpleAudioEngine

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). 所以我正在尝试使用SimpleAudioEngine在我的Cocos2D游戏中播放一些效果,但在我添加它们后,我的应用程序在进入后台时崩溃(多任务)。 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): sgx错误(不允许后台gpu访问):

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. 还有一件事,当我在模拟器上运行我的应用程序,或者甚至在我的设备上仔细调试时(在应用程序运行时逐行与XCode一起运行),这不会发生。

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 ) . 我通过使用bool来检查应用程序是否正在运行,或者在应用程序转到前台(applicationWillEnterForeground)时将其设置为true,并在应用程序转到后台(applicationDidEnterbackground)时将其设置为false。 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). 所以使用bool你可以判断应用程序是否在后台,如果是,我只是退出EAGLView中的drawView函数(因此没有做任何导致错误的图形渲染)。

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 我不需要卸载和重新加载我的声音或任何东西,我的应用程序现在有多任务XD

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(); 默认情况下,当我启动我的项目时, CCDirector :: sharedDirector() - > pause(); and CCDirector::sharedDirector()->resume(); 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 . 分别分别为(UIApplication的*)AppController.mm,一旦通过的AppDelegate :: applicationDidEnterBackground()的AppDelegate :: applicationWillEnterForeground(:双双被称为两次,一次是通过(无效)applicationWillResignActive:(UIApplication的*)(无效)applicationDidBecomeActiveAppDelegate.cpp中

Make absolutely sure that these methods are only being called once, in AppController.mm. 确保在AppController.mm中只调用这些方法一次。 In AppDelegate.cpp, instead make sure that you are calling CCDirector::sharedDirector()->stopAnimation(); 在AppDelegate.cpp中,确保您正在调用CCDirector :: sharedDirector() - > stopAnimation(); in place of CCDirector::sharedDirector()->pause(); 代替CCDirector :: sharedDirector() - > pause(); and CCDirector::sharedDirector()->startAnimation(); CCDirector :: sharedDirector() - > startAnimation(); in place of CCDirector::sharedDirector()->resume(); 代替CCDirector :: sharedDirector() - > resume(); .

Hope that's helpful to anyone else stuck in this crappy situation! 希望这对陷入这种糟糕局面的其他人有所帮助!

I was experiencing this, on about 25% of the occasions that my application re-entered the foreground. 我正在经历这种情况,大约25%的情况下我的应用程序重新进入前台。 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. 我很想知道这是否有帮助。

I had the same issue in my application and spent some 4 hours to find out. 我的申请中遇到了同样的问题,花了大约4个小时才发现。 Going background was OK the first time, but crash application the second time. 第一次去的背景还可以,但是第二次崩溃应用程序。 With a short error message related to OpenGL. 使用与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. 我发现前景是在我的自定义电平表类中创建2个定时器。 I had registered UIApplicationWillEnterForegroundNotification and UIApplicationWillResignActiveNotification . 我注册了UIApplicationWillEnterForegroundNotificationUIApplicationWillResignActiveNotification Then, going background invalidated only one, since I registered only on notification... That was it! 然后,去背景只有一个无效,因为我只在通知上注册...就是这样!

One need's count its notifications! 一个需要计算它的通知!

Are you sure it's related to audio? 你确定它与音频有关吗? "background gpu access" sounds like it's using OpenGL. “background gpu access”听起来像是在使用OpenGL。

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

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