Reputation: 869
Below is my code i unable to play SnareHitSample.wav file. i don't know where i am getting wrong.
-(void)playAudioFiles
{
NSString * path;
AVAudioPlayer * snd;
NSError * err;
NSString *name;
name = @"SnareHitSample.wav";
path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL * url = [NSURL fileURLWithPath:path];
snd = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&err] ;
if (! snd) {
NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
} else {
[snd prepareToPlay];
}
} else {
NSLog(@"Sound file '%@' doesn't exist at '%@'", name, path);
}
}
Upvotes: 1
Views: 11021
Reputation: 869
thank everyone i declared avaudioplayer object in .h files my issue get solved.
{
AVAudioPlayer *player1;
}
Upvotes: 7
Reputation: 264
try Below Code:
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"SnareHitSample" ofType:@"wav"]] error:NULL];
[theAudio play];
and you will get rid of this.. any query revert me..
Upvotes: 1
Reputation: 2919
Try this code.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"SnareHitSample"
ofType:@"wav"]];
NSError *error = nil;
AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
}
else
{
audioPlayer.delegate = self;
[audioPlayer play];
[audioPlayer setNumberOfLoops:INT32_MAX]; // for continuous play
}
Upvotes: 5