I'm try to implement ability to change interface in my program, something like themes. I'm decide to use bundles, so in my case they looks like this:
Every bundle have folders:
So I can get path to any required picture from bundle with this code:
path = [NSString stringWithFormat:@"%@.bundle/graphic/%@", selectedBundle, imageName];
When I get pictures from bundle there is no problem, path looks like this (I check it using breakpoints): Theme1.bundle/graphic/pic1.png and I create UIImage
image = [UIImage imageNamed:path];
Pictures loaded without any problem.
But when I trying to play sound using AVAudioPlayer
, I get path to sound by same way and path looks like this: Theme1.bundle/sound/sound1.wav , but when I try to create NSData
sound = [NSData dataWithContentsOfFile:path];
it is not initialized.
When I try to keep sounds in main bundle and get it with
path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
everything is OK, and path looks like this
/var/mobile/Applications/E8313E88-CE05-44B2-A80C-B05331D8596F/Myapp.app/sound1.wav
I cant understand why this works with pictures but not with sound?
Why I cant get sound from bundle? Only one thing I can suggest - imageNamed:
do some work to find right path to file and dataWithContentsOfFile:
does not.
In your code instead of
sound = [NSData dataWithContentsOfFile:path];
Use
NSString * path = [NSString stringWithFormat:@"%@.bundle/sounds/%@", selectedBundle, soundName];
NSString *fullPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:path];
sound = [NSData dataWithContentsOfFile:path];
Explanation: ImageNamed
attaches the path of the application to the image name you specify
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.