Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to play mp4 videos in AVPlayer

enter image description hereI'm using AVPlayer to play videos. I couldn't play .mp4 videos.I'm getting 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];`
like image 870
Bharu Avatar asked Jul 29 '16 13:07

Bharu


1 Answers

Try this code I think it would be working for you, because It seems your URL is not for bundle its from web.

// 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];
like image 168
Ravi B Avatar answered Sep 29 '22 18:09

Ravi B