What is the difference between URLWithString and the URLLithithath file from NSURL?

In my code, I have to use URLWithString to play streaming video ( HLS ) and fileURLWithPath to play local video.

What is the difference between these two methods? How to use one method to play both videos.

I also need to show the last frame as a still image when the HSL video ends. Now it shows a blank screen when it ends. How should I achieve this?

+14
ios objective-c path nsstring nsurl
Apr 19 '13 at 4:50
source share
2 answers

+URLWithString: creates an NSURL that represents the string as given. So the string could be @"http://www.google.com" , and the URL represents http://www.google.com .

+fileURLWithPath: takes a path, not a URL, and creates an NSURL that represents the path using the URL file:// . Therefore, if you give /foo/bar/baz , the URL will represent file:///foo/bar/baz .

You can, of course, manually create the file URL string and pass it to +URLWithString: but +fileURLWithPath: easier to use when you already have the path, since you don't have to deal with the string escape and forcing it in the URL format.

+15
Apr 19 '13 at 4:53 on
source share

A similar event occurred in my application that uses AVAudioPlayer. I tried with [NSURL URLWithString:path] and found that it could not open certain mp3 files. I searched for an error in a line like [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] fileTypeHint:AVFileTypeMPEGLayer3 error:&error] , but the error was just nil

However, it was resolved by replacing the url with [NSURL fileURLWithPath:path] .

In both cases, the path path NSString * @"/var/mobile/Containers/Data/Application/4D96D4AE-2ED4-40B0-85D2-230E1AFA90E7/Documents/01-AudioTrack 01.mp3" 0x1457a8f0 However, I do not know the reason, but now I have to be careful using [NSURL URLWithString:] .

PS. In a NSURL reference document, Apple stated the following:

IMPORTANT To create NSURL objects for file system paths, use fileURLWithPath: isDirectory: instead.

which explicitly specifies [NSURL fileURLWithPath:] should be used for an open file, although [NSURL URLWithString] also works in some cases.

+1
Mar 14 '15 at 16:53
source share



All Articles