Search code examples
objective-cnsbundle

Objective C - Get file path in subfolder of supporting files


I'm trying to play an mp3 that's in a couple of subfolders:

NSString *fName = @"subfolder1/subfolder2/mp3name";
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:fName ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[myAudioPlayer play];

The mp3 will play if it's not in a subfolder, but it won't play as above in a subfolder. How can I get the mp3 to play from a subfolder?


Solution

  • You need to use a different method so you can specify the path:

    - (NSString *)pathForResource:(NSString *)name
                           ofType:(NSString *)extension
                      inDirectory:(NSString *)subpath