使用AFnetworking来保存mp4

时间:2013-06-15 11:46:10

标签: objective-c video afnetworking mp4

我一直在使用AFnetworking从远程服务器下载视频。除了将文件作为数据而不是原始格式(在本例中为mp4)下载之外,它的效果很好。

有没有办法将AFnetworking设置为mp4下载文件?在播放时,每次播放视频时,将数据文件转换回mp4似乎是浪费时间和资源。

我目前获取文件的代码如下:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[URL url]]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[URL title]];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
        URL.videolocal = @"YES";
        [self saveToCoreData]; // video is not saved in core data, only a reference 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation start];

1 个答案:

答案 0 :(得分:0)

如果来自服务器的数据已经是MP4格式,那么只需使用.mp4后缀命名该文件。