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:
Did you
synthesize
your button after decalring an@property
for it?Does
95.wav
exist in your Xcode Project (have you imported it)?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.
精彩评论