7

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];`
Bharu
  • 159
  • 1
  • 3
  • 13
  • Using `NSLog` or debug point check filepath or soundFilePath have a proper value or not? Also provide more detail like error log you got. – Ravi B Jul 29 '16 at 13:30
  • 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' – Bharu Jul 29 '16 at 13:32
  • 1
    So your URL is wrong. `self.video_Array ` in this array which URLs are there, its a HTTP link or its a local url. – Ravi B Jul 29 '16 at 13:36
  • Give me one of your URL. if possible, because according your code it seems you are trying to play video which are in your `bundle`. – Ravi B Jul 29 '16 at 13:37
  • file path is right, but sound filePath shows nil value. check my above code – Bharu Jul 29 '16 at 13:39
  • You need to provide `filePath` so that I can be help you. – Ravi B Jul 29 '16 at 13:41

1 Answers1

3

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];
Ravi B
  • 1,574
  • 2
  • 14
  • 31