What is difference between URLWithString and fileURLWithPath of NSURL?

前端 未结 2 362
失恋的感觉
失恋的感觉 2020-12-03 05:20

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

What is the diff

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 06:14

    Similar thing happened in my app which use AVAudioPlayer. I tried with [NSURL URLWithString:path] and found out it fails to open certain mp3 files. I looked into error by a line like [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] fileTypeHint:AVFileTypeMPEGLayer3 error:&error], but the error was simply nil

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

    In both case, the path path NSString * @"/var/mobile/Containers/Data/Application/4D96D4AE-2ED4-40B0-85D2-230E1AFA90E7/Documents/01-AudioTrack 01.mp3" 0x1457a8f0 Still I don't know the reason but now I should be careful using [NSURL URLWithString:].

    PS. In NSURL Reference document, Apple said as below:

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

    which clearly indicates [NSURL fileURLWithPath:] should be used for open file, though [NSURL URLWithString] also works for some cases.

提交回复
热议问题