Cocos2d: thread error when trying to add background music - ios

Hello I am making a cocos2d side scroller. I am trying to add a background audio file. I am using SimpleAudioEngine. This is the code:
[[SimpleAudioEngine sharedEngine]playBackgroundMusic:#"Soundtrack.mp3" loop:YES];
But when I run it it gives me a error. The line that it says is giving me the error is:
[audioSourcePlayer prepareToPlay];
Is there a different way to add a sound file that won't give me this error?

The AVAudio and OpenAL will throw some exceptions, it's normal that the Xcode exception breakpoint triggers and nothing to worry about.
Instead of removing the exception breakpoint I highly recommend to "edit" the breakpoint and from the tiny edit dialog make sure you select that only Objective-C exceptions are caught. That way all the audio exceptions won't bother you (they're C++ exceptions) but you can still see where things crash should an assertion trigger.

Related

Detect which instruction may cause error in my app ios

I'm new with Ios programming Objective-C language and my app was working great until suddenly stop working and get this error
Xcode show me a generic error and i want to know which function or instruction produce this error i couldn't find how to do this with Xcode i want just to point the problem where it may come from
This is the error i got :
Add Exception breakpoint
As shown in the image, select the 8th tab(Exception) and from the below add button, add the Exception Breakpoint. This will show the exact line of code from where the application crashes.
Add Exception breakpoint
How to create exception breakpoints in Xcode?
Exception breakpoints are a powerful debugging tool that remarkably
few people know about, so please read the following carefully and put
it into practice!
A regular breakpoint is on a line you specify and causes the debugger
to pause execution at that point so you can evaluate your program's
state. An exception breakpoint tells the debugger to pause whenever a
problem is encountered anywhere in your program, so you can evaluate
your program's state before it crashes.
Exception breakpoints are trivial to set up: go to the Breakpoint
Navigation (Cmd+8), then click the + button in the bottom left and
choose Add Exception Breakpoint. You can leave it there if you want
to, but it's preferable to make one further change to reduce
unnecessary messages: right-click on your new breakpoint, choose Edit
Breakpoint, then change the Exception value from "All" to
"Objective-C".
Please follow the below steps.
Run your project.

ios - music player stops as soon as I enter my app

I am tasked to finding why this app stops the Music Player on my ipod. Currently, in XCode, i have set breakpoints in the very first line of main(), and still, when i reach that line, the Music Player has already stopped.
I can imagine that any static constructor would have already run, but I've searched for the obvious culprits (any mention of AVAudioSession), and found nothing that had run before main().
Since the codebase is just huge, it would be a pain to blindly search for every constructor in every file, without knowing what I'm looking for.
Can you tell me if there is any kind of XCode project property that stops external audio from playing, or anything that I could look for?
Thanks
EDIT: I've narrowed it down a lot.
First, for some reason, XCode seems to ignore most of my breakpoints which mislead me completely.
It was indeed a static initialization of an object, which eventually leads to this call:
newDevice = alcOpenDevice(NULL);
For some reason, I can actually get a breakpoint here. Go figure...
So, I have the music playing on this line, but not after I run it. It seems openal's initialization of the device shuts off all external music, for some reason... Trying to understand why, and how can i circumvent it...
Check the documentation for AVAudioSession's Categories, it sounds like your app has a nonmixable AVAudioSessionCategory, can you reproduce if you change it to AVAudioSessionCategoryAmbient?
Have you tried setting the music to play on load through the AppDelegate ?
Try having it so if the user loads back into the app it continues playing music.
Ok, I found out the problem. For some reason, XCode wasn't helping out. So I had set lots of breakpoints everywhere to see what would run before getting to main. This is where XCode failed me, because I knew the code was running, but for some reason, the breakpoints weren't being triggered. Also, for some reason, others did, so I still have that particular mistery to solve.
Apart from that, the problem was initializing OpenAL just before AVAudioSession got configured with AVAudioSessionCategoryAmbient. After switching those two around, everything worked just fine.
Thank you all for your help!

Always stop in App delegate after enabling All exceptions break point

When i enable all exceptions breakpoint my app always stops in AppDelegate, but im able to continue the program execution, but its very annoying cause always takes me to the appdelegate. Any ideas why?
Only enable Objective-C breakpoints.
To see the actual statement that is causing the error add an exception breakpoint:
From the Main Menu Debug:Breakpoints:Create Exception Breakpoint.
Right-click the breakpoint and set the exception to Objective-C. This will ignore other types of exceptions such as from C++. There are parts of the APIs that use exceptions such as Core Data (Apple is Special).
Add an action: "po $arg1".
Run the app to get the breakpoint and you will be at the line that causes the exception and the error message will be in the debugger console.
Breakpoint example:
If you're using Swift, or you want to have all the exceptions captured, you can change an option on All Exceptions to automatically continue after evaluating actions. Just find it in the Breakpoint Navigator and right/ctrl click the all Exceptions Breakpoint to edit it:
Then check the Options box:
Exceptions in C++ code common and normal. The exception breakpoint catches every raised exception even when they are being handled correctly. So if you don't specify Obj-C only you will notice that execution stops in a lot of seemingly random places. I run into this all the time with AVAudioPlayer especially.
Another thing to look out for are missing assets. I came across this question from another asker who seems to have also run into the same issue.
Xcode throws an exception in Main() in iOS 8 with 'all exceptions' breakpoint

Xcode - turn off debug

Is it possible to turn off debug mode for a specific .m file ?
Summary.
I am writting a framework which will be used in other projects, code contains #try #catch blocks, which handles some exceptions.
Problem.
If framework user turns on Xcode's "All exceptions" breakpoints, then LLDB periodically stops program (when exception is raised) in #try block.
What I want.
I want to mark my framework files "non sensitive" for lldb (ignore exception breakpoints).
Solutions that I have already tried.
I tryed python script which checks eax/r0 and ignores exception, also a variant with breakpoint condition, but all this solutions are not nice for thousand of newbie third party developers.
Is it possible to have some nice solution?
Thanks.
This previous answer demonstrates how you can create a conditional exception breakpoint for your project. It may be a lot of work to specifically exclude each one of your framework's classes.

NSException rethrowing with wrong stack in the debugger

When I crash (for reasons I understand; that's not the problem) when I try to do something Cocoa isn't okay with, such as calling a method that doesn't exist or attempting to insert nil into a set, the debugger shows the stack from main() to __pthread_kill, without any of the frames that were present when the actual crashing code ran. There is a frame (9th from main) called objc_exception_rethrow. This leads me to believe that Cocoa Touch is trying to do something or other to recover all exceptions and die gracefully or something. However, it is very irritating when debugging to not have the ability to actually use Xcode4's debugging tools to investigate the calling stack frames, or even see where in my code I crashed.
Is there some way to make the objc_exception_rethrow behavior not happen, and just crash as soon as an exception is raised? Perhaps there's a debug setting that makes it crash earlier (at the right time)? (I haven't messed with any of the build settings in this project yet.)
I don't know any Xcode setting that could disable re-throwing exceptions. To my knowledge they are re-thrown by the runtime. You could try running the app without the debugger attached and let it crash. The crash report should contain a section "Last Exception Backtrace" which will give you exactly what you need in this case.
I found the answer: set a breakpoint on Obj-C Exceptions. It will go to the debugger when objc_exception_throw is hit, which is good. Unfortunately, this happens before the exception is printed, but we can make that happen anyway (most of the time) by setting the breakpoint's action to be (Debugger Action) po *(id *)($ebp + 8).

Resources