Matt
Matt

Reputation: 2930

pathForResource from URL

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?

Upvotes: 0

Views: 1075

Answers (3)

avpaderno
avpaderno

Reputation: 29669

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];

Upvotes: 0

user1040049
user1040049

Reputation:

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).

Upvotes: 2

Fran Sevillano
Fran Sevillano

Reputation: 8163

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.

Upvotes: 1

Related Questions