I’m new to this, so in an attempt to keep things simple, I’m trying to add sound into my main.m
As far as I know, the following code should work, but when I run it, I get a “Error Domain=NSOSStatusErrorDomain Code=-43 “The operation couldn’t be completed. (OSStatus error -43.)” (File not found)”
I’ve tried it with various audio files and formats – added to the project in Xcode but can’t figure out why it isn’t finding my G1.wav
AVAudioPlayer *audioPlayer;
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"/G1.wav"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = -1;
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
EDIT removed NSBundle and mainBundle references – my misunderstanding
You might be better off with this:
Using
pathForResource:ofType:you don’t have to worry about where in the hierarchy of your bundle the file is. The API call figures it out. And in your code, whateverresourcePathis returning is clearly not the right path.You might add some
NSLog()calls here and there to see what is really happening, as well.