URLWithString returns nil for the resource path - iphone

The problem with getting the resource URL is for some reason: this code is in viewDidLoad, and it worked in other applications, but not for any 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 LINE: /var/mobile/Applications/D9FA0569-45FF-4287-8448-7EA21E92EADC/SoundApp.app/sound.wav

AUDIO URL: 0

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

+7
iphone resources nsurl
source share
4 answers

There is no protocol in your line, so this is an invalid url . Try it...

 NSString* expandedPath = [audioString stringByExpandingTildeInPath]; NSURL* audioUrl = [NSURL fileURLWithPath:expandedPath]; 
+22
source share

Just change one line to the following:

  NSURL* audioURL = [NSURL fileURLWithPath:audioString]; 
+6
source share

You pass audioURL to your NSLog method as %d , therefore why you get 0. If you pass it as an object with %@ , you will get NULL .

Try switching to audio players like this and skip the line.

 NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"]]; 
+4
source share

You do not read the answers carefully enough - you use URLWithString when you should use fileURLWithPath. You cannot pass file: // path to URLWithString. I think you also need to add the file: // at the beginning of the line, since you only have the path (which, as indicated, has no protocol).

+2
source share

All Articles