Search code examples
iosobjective-cvideoavplayeravplayeritem

Unable to play downloaded video on ios app


I am working on iOS application. There I want to play a video which is stored on my local filesystem. So I am using AVPlayer. But when I try to play the video I always see the play symbol crossed. Here is my code:

- (void)playVideo:(NSURL *)videoUrl onView:(UIView *)view{
   videoUrl = [videoUrl URLByAppendingPathExtension:@"3gp"];
   NSLog(@"file url: %@", videoUrl);
   AVURLAsset *asset = [AVURLAsset assetWithURL:videoUrl];
   AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
   AVPlayer* playVideo = [[AVPlayer alloc] initWithPlayerItem:playerItem];
   AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
   playerViewController.player = playVideo;
   playerViewController.player.volume = 0;
   playerViewController.view.frame = view.bounds;
   [self.viewController addChildViewController: playerViewController];
   [self.viewController.view addSubview:playerViewController.view];
   [playVideo play];
}

Couple of things to add:

If I print url I see this:

file:///var/mobile/Containers/Data/Application/93D7BE2E-2622-4614-9E8B-2DA07593D68D/Documents/2b6f779a63e9d714944f66f01f05a3b437b79f43

This is why I am adding manually extension

videoUrl = [videoUrl URLByAppendingPathExtension:@"3gp"];

Also if I print the status of player item it always prints 0 -> undefined

NSLog(@"item status: %ld", playerItem.status);

Any ideas about where the error is?

Thanks in advance


Solution

  • I am sharing some links, I think this will solve your issue.

    1.look at this answer

    2. The another one.