I’m trying to play a mp3 file using AVFoundation framework. It can’t detect the file and has the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
Code implemented:
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
NSLog(@"directory %@",path);
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
When I change the pathForResource:@”sound” to pathForResource:@”sound ” (the space ) it can detect the file but no sound heard. I have declared the AVdelegate in the .h file too. Anyone knows whats the problem?
You need to gige prepareToPlay first for the audio to play.. check this code snippet.. it works for me.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
}