I currently have this code that works. I have two questions.
- How do I set the directory to “Supporting Files”?
- How do I use a variable in the file name? I need it to pull an mp3 of any animal based on a variable.
- (void)playAnimal{ NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Sheep.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 0; if (audioPlayer == nil) NSLog([error description]); else [audioPlayer play]; }
I’m very new to Objective-C, so a lot of syntax things are new to me as well as general methods. Thanks for your patience!
Use the following:
When your resources are copied to the bundle, they do not retain their directory structure. Just identify the resource by name and it will find the path for it.
Just as an FYI, the following works, as well:
This may be useful if you have a set of resources logically grouped together in a plist and you are programmatically reading those out. For example: