Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLWithString returns nil for resource path - iphone

Having a problem getting the URL for a resource for some reason: This code is in viewDidLoad, and it's worked in other applications, but not here for some reason:

NSString* audioString = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
NSLog(@"AUDIO STRING: %@" , audioString);

NSURL* audioURL = [NSURL URLWithString:audioString];
NSLog(@"AUDIO URL: %d" , audioURL);

NSError* playererror;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&playererror];
[audioPlayer prepareToPlay];    

NSLog(@"Error %@", playererror);

LOG OUTPUT:

AUDIO STRING: /var/mobile/Applications/D9FA0569-45FF-4287-8448-7EA21E92EADC/SoundApp.app/sound.wav

AUDIO URL: 0

Error Error Domain=NSOSStatusErrorDomain Code=-50 "Operation could not be completed. (OSStatus error -50.)"

like image 984
Adam Avatar asked Jan 21 '10 21:01

Adam


3 Answers

Your string has no protocol, so it's an invalid url. Try this...

NSString* expandedPath = [audioString stringByExpandingTildeInPath];
NSURL* audioUrl = [NSURL fileURLWithPath:expandedPath];
like image 184
slf Avatar answered Nov 13 '22 17:11

slf


Just change one line to this:

    NSURL* audioURL = [NSURL fileURLWithPath:audioString];
like image 41
willc2 Avatar answered Nov 13 '22 19:11

willc2


You're passing audioURL in your NSLog method as %d hence why you get 0. If you pass it as an object with %@ you'll get NULL.

Try passing into the audioplayer like this and skip the string.

NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]];
like image 4
Convolution Avatar answered Nov 13 '22 17:11

Convolution