Reputation: 69
I am trying to play a sound on the click of a button. But when I run the app it crashes and I get Thread 1 : signal SIGABRT
.
This is my code:
.h file
#import <AVFoundation/AVFoundation.h>
@interface HljodViewController : UIViewController <AVAudioPlayerDelegate>{
}
-(IBAction)playsound;
.m file
-(IBAction)playsound {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Geese_4" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.numberOfLoops = 1;
[theAudio play];
}
Upvotes: 0
Views: 179
Reputation: 6079
try this:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
NSString *path = [[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"mp3"];
NSError *error;
AVAudioPlayer *sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[sound play];
Upvotes: 0
Reputation: 11725
No one can tell you how to solve your issue if you don't capture the NSError
. I suggest you do the following:
NSError *outError = nil;
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&outError];
if (outError)
{
NSLog(@"%@", [outError localizedDescription]);
}
... // continue on to happy time
Upvotes: 1