Unable to play mp4 videos in AVPlayer

enter image description here I am using AVPlayer to play videos. I could not play the .mp4 video. I get this black screen. here is my code.

`NSString *filePath = [self.videoArray objectAtIndex:index]; NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: @"mp4]; url = [[NSURL alloc] initFileURLWithPath: soundFilePath]; playerViewController = [[AVPlayerViewController alloc] init]; playerViewController.videoGravity = AVLayerVideoGravityResize; playerViewController.view.frame = self.view.bounds; playerViewController.showsPlaybackControls = NO; video=[AVPlayer playerWithURL:url]; playerViewController.player = _video; playerViewController.view.userInteractionEnabled = false; [video play]; [self.view addSubview:playerViewController.view];` 
+6
source share
1 answer

Try this code, I think it will work for you, because it seems that your URL is not linked to its website.

 // your filepath which is may be "http" type NSString *filePath = [self.video_Array objectAtIndex:index]; // http to NSURL using string filepath NSURL *url= [[NSURL alloc] initWithString:filePath]; AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init]; playerViewController.videoGravity = AVLayerVideoGravityResize; playerViewController.view.frame = self.view.bounds; playerViewController.showsPlaybackControls = NO; video=[AVPlayer playerWithURL:url]; playerViewController.player = _video; playerViewController.view.userInteractionEnabled = false; [video play]; [self.view addSubview:playerViewController.view]; 
+5
source

All Articles