开发者

My application crashes when i touch a specific button

开发者 https://www.devze.com 2023-02-21 13:29 出处:网络
Here is the header for my button. IBOutlet UIButton *buttonOneOne; } - (IBAction)buttonOneOne:(id开发者_如何学C)sender;

Here is the header for my button.

IBOutlet UIButton *buttonOneOne;

}

- (IBAction)buttonOneOne:(id开发者_如何学C)sender; 

@property (nonatomic, retain)   IBOutlet UIButton       *buttonOneOne;

This is what i put in my .m file for it

- (IBAction)buttonOneOne:(id)sender {

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"95" ofType:@".wav"];

NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];

NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];

[musicPlayer prepareToPlay];
[musicPlayer setVolume:.8];
[musicPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[musicPlayer setDelegate: self];
[musicPlayer play];

}

why is it crashing?


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' means that the file you're referencing cannot be found.

You should not use the dot when specifying the file extension.

Change ofType:@".wav"
to ofType:@"wav"

NSBundle Class Reference


You should post some console output (possibly a stack trace), it would help us a LOT more.

Possible causes of a crash I can see from this code:

  1. Did you synthesize your button after decalring an @property for it?

  2. Does 95.wav exist in your Xcode Project (have you imported it)?

  3. Are you using Interface Builder? If so, check your IB project for old IBOutlets and IBActions, as once you rename a function or whatnot, you're bound to get interface elements pointing to old code.

0

精彩评论

暂无评论...
验证码 换一张
取 消