无法使用 AVPlayerViewController 在 iPad 上播放视频文件

Not able to play video file on iPad using AVPlayerViewController

我在这里看到了完全相同的问题,但没有得到解答,所以再次问同样的问题。 我尝试了各种方法来尝试播放视频,但不幸的是它们对我不起作用。 这是代码:

AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

NSString *url = [[NSBundle mainBundle] pathForResource:@"Intro" ofType:@"mp4"];

_asset = [AVURLAsset assetWithURL: [NSURL URLWithString:url]];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
_player = [[AVPlayer alloc] initWithPlayerItem: _playerItem];
[playerViewController.view setFrame:_videoView.frame];

playerViewController.showsPlaybackControls = YES;

[_videoView addSubview:playerViewController.view];
playerViewController.player = _player;
[_player play];

这里是使用上面的代码显示当前情况的屏幕截图。我使用另一个 Apple 代码来检查视频,它工作正常,但 Apple 代码对我来说太复杂了,因为它对于简单的事情来说太复杂了。这是:https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationSimplePlayer-iOS/

谢谢 阿杰

我终于能够通过再次查看 Apple 的示例代码来解决这个问题。 解决方案: 将 AAPLPlayerView 文件添加到我的项目中。 然后将 Storyboard 中的 UIView 更改为上面 class 然后将下面的代码行更新到我的视图控制器 class:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.playerView.playerLayer.player = self.player;

    NSURL *movieURL = [[NSBundle mainBundle] URLForResource:@"Intro" withExtension:@"mp4"];
    self.asset = [AVURLAsset assetWithURL:movieURL];

    self.playerItem = [AVPlayerItem playerItemWithAsset: self.asset];
    [self.player play];
}

- (AVPlayer *)player {
    if (!_player)
        _player = [[AVPlayer alloc] init];
    return _player;
}
- (AVPlayerLayer *)playerLayer {
    return self.playerView.playerLayer;
}

- (AVPlayerItem *)playerItem {
    return _playerItem;
}
- (void)setPlayerItem:(AVPlayerItem *)newPlayerItem {
    if (_playerItem != newPlayerItem) {

        _playerItem = newPlayerItem;

        // If needed, configure player item here before associating it with a player
        // (example: adding outputs, setting text style rules, selecting media options)
        [self.player replaceCurrentItemWithPlayerItem:_playerItem];
    }
}

它奏效了。 问题是 PlayerLayer 设置不正确,因此视频不可见。