无法在AVPlayer中播放mp4视频

时间:2016-07-29 13:26:28

标签: objective-c video avplayer avplayerviewcontroller

enter image description here我正在使用AVPlayer播放视频。我无法播放.mp4视频。我得到了这个黑屏。这是我的代码。

`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];`

1 个答案:

答案 0 :(得分:2)

尝试使用此代码我认为它会对您有用,因为您的网址似乎不是来自网络的。

// 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];
相关问题