Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中检索下载的内容?_Ios_Objective C - Fatal编程技术网

如何在iOS中检索下载的内容?

如何在iOS中检索下载的内容?,ios,objective-c,Ios,Objective C,在我的应用程序中,我下载了一个mp3文件,并通过指定文件名写入abc.mp3来存储该文件,存储时显示的文件路径如下: /var/mobile/Containers/Data/Application/9AFC500A-91D5-421B-8A5F-19A239456353/Documents/abc.mp3 当我玩它的时候,路径是 file:///var/mobile/Containers/Data/Application/9AFC500A-91D5-421B-8A5F-19A239456353

在我的应用程序中,我下载了一个mp3文件,并通过指定文件名写入abc.mp3来存储该文件,存储时显示的文件路径如下:

/var/mobile/Containers/Data/Application/9AFC500A-91D5-421B-8A5F-19A239456353/Documents/abc.mp3
当我玩它的时候,路径是

file:///var/mobile/Containers/Data/Application/9AFC500A-91D5-421B-8A5F-19A239456353/Documents/abc.mp3
要播放的代码是

NSURL* songUrl = [NSURL fileURLWithPath:songUrlString];
mediaPlayer = [[AVPlayer alloc]initWithURL:url ];  //This is AVPlayer Instance
[mediaPlayer play];
要下载的代码是:

-

所以最后的问题是没有播放内容。我甚至不知道内容是否写入文件。

系统会“给”您一个任意的文档文件夹(即
NSDocumentDirectory
),您无法保证它的完整路径。 要解决此问题,您应该在每次需要文件时“构建”路径,就像编写文件时那样:

//build the path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"abc.mp3"];

//then you can do:
NSURL* songUrl = [NSURL fileURLWithPath: path];
mediaPlayer = [[AVPlayer alloc]initWithURL:url ];
[mediaPlayer play];

请注意,我只是使用了您在下载文件时用来构建路径的代码。

检查第一个文件是否由NSData*data成功写入=[[NSFileManager defaultManager]contentsAtPath:path];如果数据为零,则文件在上不可用path@johnykumar它的工作。。。你是个了不起的人。。。你写下答案,我会接受你的,因为你是first@kb920谢谢你,伙计。。。
//build the path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"abc.mp3"];

//then you can do:
NSURL* songUrl = [NSURL fileURLWithPath: path];
mediaPlayer = [[AVPlayer alloc]initWithURL:url ];
[mediaPlayer play];