Как воспроизвести загруженное видео

#ios #video #afnetworking #mpmovieplayercontroller

#iOS #Видео #afnetworking #mpmovieplayercontroller

Вопрос:

Я загрузил видео с URL-адреса, используя AFNetworking и установив пути к видео. Затем доступ к пути для воспроизведения видео. Но когда я нажимаю кнопку для воспроизведения видео, ничего не происходит.

Скачать видео:

 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:amp;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:

Ваш путь к видео выглядит следующим образом

 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