0

I have this code:

NSString *songPathForm = @"http://www.example.com/file.wav";

NSString *song = [[NSBundle mainBundle]pathForResource:songPathForm ofType:nil];

But song just ends up being null, what am I doing wrong?

Matt
  • 2,920
  • 6
  • 23
  • 33
  • What are you trying to achieve? Your code is passing a URL to a method, to get a path. As you have already the URL, why do you need a path? – apaderno Dec 08 '11 at 01:05
  • @kiamlaluno I am pulling in audio links with a rss feed to play with AVFoundation framework, I tried playing them just with the NSString songPathForm, but it won't work that way – Matt Dec 08 '11 at 02:08

3 Answers3

2

A 'path' here is meant as a file path on the local file system. What you're giving to NSBundle is a URL. NSBundle is typically used to get files which are in the application itself (What you see when you select 'Show Package Contents' from the Finder). NSBunlde is expecting a relative file path also, not an absolute path (because you don't know where the app is).

1

The pathForResource method works only for files inside a bundle. If you want to access a file at a external URL you could use dataWithContentsOfURL: in the NSData class for example.

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
  • When I use this: NSData* song = [NSData dataWithContentsOfURL: [NSURL URLWithString:songPathForm]]; I get a bunch of crap being outputted, like 004d0000 000e00004 and stuff – Matt Dec 07 '11 at 23:49
  • @Matt That is not crap; it's the content of the file. – apaderno Dec 08 '11 at 01:06
  • It doesn't work though, I am using this to play a music file, that NSData cannot be played – Matt Dec 08 '11 at 01:54
  • Ok, I think you should do a little research before asking. I don't know what you are using to play the sound, but if you use `AVAudioPlayer` you can make use of the `initWithData:error:` method that will play a sound stored in a `NSData`. – Fran Sevillano Dec 08 '11 at 10:25
0

To play a sound for which you know the URL, you should use code similar to the following one:

NSSound *sound = [[NSSound alloc] initWithContentOfURL: [NSURL URLWithString:songPathForm] byReference:NO];
[sound play];
apaderno
  • 28,547
  • 16
  • 75
  • 90