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

时间:2015-11-18 23:49:22

标签: ios objective-c ipad avkit

我在这里看到了完全相同的问题,但没有答案,所以再次提出同样的问题。 AVPlayer wont play video from url in iOS9 我尝试了各种各样的方法来尝试播放视频,但遗憾的是它们并不适合我。 这是代码:

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/

enter image description here 谢谢 AJ

1 个答案:

答案 0 :(得分:0)

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

- (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设置不正确,因此视频不可见。