我正在使用AVPlayer播放视频。我在全屏模式下有问题。视频播放器来自左上角。看起来很奇怪。

NSString *filePath = [self.video_Array objectAtIndex:index];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:filePath ofType: nil];
url = [[NSURL alloc] initFileURLWithPath: soundFilePath];
currentItem = [AVPlayerItem playerItemWithURL:url];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.videoGravity = AVLayerVideoGravityResize;
_playerViewController.view.frame = self.view.bounds;
_playerViewController.showsPlaybackControls = NO;
_video=[AVPlayer playerWithURL:url];
_video = [AVPlayer playerWithPlayerItem:currentItem];
_playerViewController.player = _video;
[_playerViewController.player play];
[self.view addSubview:_playerViewController.view];

最佳答案

请参考我的代码,该代码在我的应用程序中正常运行,

 -(void)videoPlayer:(NSString *)filePath{

    playerViewController = [[AVPlayerViewController alloc] init];
    self.viewPlayerContainer.frame = CGRectMake(0, 74, self.view.bounds.size.width, self.view.bounds.size.width);

    NSURL *url = [NSURL fileURLWithPath:filePath];

    AVURLAsset *asset = [AVURLAsset assetWithURL: url];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

    AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
    playerViewController.player = player;
    playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [playerViewController.view setFrame:CGRectMake(0, 0, self.viewPlayerContainer.bounds.size.width, self.viewPlayerContainer.bounds.size.height)];
    [playerViewController addObserver:self forKeyPath:@"videoBounds" options:NSKeyValueObservingOptionNew context:nil];

    playerViewController.showsPlaybackControls = YES;

    [self.viewPlayerContainer addSubview:playerViewController.view];

    [player play];
}

10-08 16:57