如何播放下载的视频

时间:2014-06-24 08:31:31

标签: ios video afnetworking mpmovieplayercontroller

我已使用AFNetworking从URL下载视频并设置视频路径。然后访问播放视频的路径。但是,当我点击播放视频按钮时,没有任何事情发生。

下载视频:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
 NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

 if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
 [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 [manager.requestSerializer setValue:@"application/x-www-form-urlencoded"
 forHTTPHeaderField:@"Content-Type"];

 [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"video/mpeg", nil];


 [manager GET:@"http://center.net/projects/AR/video.mp4"
 parameters:nil
 success:^(AFHTTPRequestOperation *operation, id responseObject) {
 [operation.responseData writeToFile:[dataPath stringByAppendingPathComponent:@"video.mp4"] atomically:YES];


 NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]);
 NSLog(@"THE RESPONSE: %@", responseObject);


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

播放视频:

path=[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"];

 mpviewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];


 [mpviewController.view setFrame:CGRectMake(0, 0, 768, 1024)];


 [self.view addSubview:mpviewController.view];

 [self presentMoviePlayerViewControllerAnimated:mpviewController];


 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedCallback:)

 name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController];

1 个答案:

答案 0 :(得分:0)

您的视频路径如下

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];
NSString *videoPath=[dataPath stringByAppendingPathComponent:@"video.mp4"]; //CORRECT

当您尝试加载错误文件时,如下所示

path=[[NSBundle mainBundle] pathForResource:@"video" ofType:@"mp4"]; //WRONG