0

Hi I have a class named as root:

In root.h :-

#import "UIKit/UIKit.h"
#import "AVFoundation/AVFoundation.h"
#import "AudioToolbox/AudioToolbox.h"

@interface root : UIView {


}
+(void)somefunction:(BOOL) sf;
@end

in root.m the definition of somefunction is as follows

-(void)somefunction:(BOOL) sf {
 //AVAudioPlayer *myExampleSound; //this variable can be named differently

 if ( issoundon==TRUE) {     
   NSString *path =[[NSBundle mainBundle] pathForResource:"bg" ofType:@"wav"];
   SystemSoundID soundID;
   AudioServicesCreateSystemSoundID(
     (CFURLRef) [NSURL fileURLWithPath:path], &soundID);
   AudioServicesPlaySystemSound(soundID);
}
else{
  NSString *path =[[NSBundle mainBundle] pathForResource:nil ofType:@"wav"];
  SystemSoundID soundID;
  AudioServicesCreateSystemSoundID(
    (CFURLRef) [NSURL fileURLWithPath:path], &soundID);
  AudioServicesPlaySystemSound(soundID);
}

now i have imported root.h in another class and i am calling the "somefunction" as follows

bool abc=true;
[root somefunction:true];

but at this point my app terminates(crashes).

basically i am trying to set background music to my app (as the game starts) and in the middle of the game i allow user to switch of the sound.(it is crashing even i am calling the function in delegate of the view.)

please tell me what is happening wrong coz my code is compiling properly(with a few warning though).

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
Rachit Singhal
  • 75
  • 1
  • 14
  • plz ignore the typos as the code is compiling correctly – Rachit Singhal Dec 06 '10 at 13:32
  • Please read the editing help and format your code properly. – Karl Knechtel Dec 06 '10 at 13:33
  • 1
    @Rachit Singhal: That is a **terrible** thing to do. Just because your code compiles correctly **doesn't mean** there's nothing wrong with the syntax. – BoltClock Dec 06 '10 at 13:51
  • Your title doesn't seem to relate to your question. – Alex Brown Dec 06 '10 at 14:01
  • When you say it's crashing - please provide the crash dump and any error messages sent to the log. – Alex Brown Dec 06 '10 at 14:02
  • 1
    You should be aware that the Cocoa naming convention has class names start with capital letters (and preferably a prefix to work around namespace issues). Calling your class `root` is very confusing, and may even lead to problems down the line when dealing with other classes that have properties named `root`. – Brad Larson Dec 06 '10 at 15:20

3 Answers3

1

Sure, your code compiles successfully. But you know what? There is a typo in your code which is causing your program to crash. Never assume your code has no typos just because it compiles successfully.

In your root.m file you have this:

-(void)somefunction:(BOOL) sf {

It should be a +, not a -, like in your header file:

+(void)somefunction:(BOOL) sf {

There might be a discrepancy between the C type bool and the Objective-C type BOOL too, but I'm not too sure about that:

bool abc=true;            // Shouldn't this be BOOL abc = YES, and
[root somefunction:true]; // shouldn't you be passing abc here?
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • `BOOL` is defined as a a `uint8_t` while `bool` is a 1-bit integer as far as i know – Richard J. Ross III Dec 06 '10 at 13:42
  • 1
    `BOOL` is a `signed char`. `true` and `YES` are both `1` (and `false` and `NO` both `0`), so there shouldn't be any issues there. – Jeremy W. Sherman Dec 06 '10 at 14:43
  • I'm betting the missing class method in the implementation is what's going on. He's probably getting warnings that should be treated as errors here. However, even if he changes the class method implementation, it looks like he wants to access the instance variable `issoundon` from within it, so he's going to need to do a little rearchitecting here. – Brad Larson Dec 06 '10 at 15:18
  • Thanks for the suggestion i will follow this from now. – Rachit Singhal Dec 07 '10 at 04:52
0

Your code is incomplete, and without context it's really hard to say what the bug might be. However:

  • You haven't defined issoundon anywhere
  • where have you constructed your root object? in interface builder?
  • why is it a subclass of UIView?
  • is AudioServicesPlaySystemSound synchronous?

But mainly, I think your problem is here:

  • why would you expect to be able to play a file called (nil).wav?

I suggest you provide a crash dump and error log to assist people in answering this question.


See this SO question: playing background audio on iphone

Community
  • 1
  • 1
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
  • My classes are "root"(in which i have defined the function "somefonction" as +(void) somefunction:(BOOL)issoundon{//..........} and the second class is "anyUIViewController.m" in which i am trying to call the function in a function named "-(IBActive)togglesound" If you could do so it will be very help full – Rachit Singhal Dec 06 '10 at 14:03
  • Your code will probably only play 30 seconds of synchronous sound, which is not 'background music'. Why is a subclass of UIView a good place to put sound code? delete the contents of the second clause of the if statement - Playing the file `nil` is not the same as muting an existing sound. – Alex Brown Dec 06 '10 at 14:24
0

"but at this point my app terminates(crashes)"

We need to know more about that to help you with this.

In the Xcode console, it should be putting out messages that would narrow in on what the problem is. A stack trace would be helpful too.

My total guess is that your local instance of root is undefined and so you're calling an unrecognized selector on that object. But without more help from the console, that's a complete guess.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87