简体   繁体   English

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

[英]Not able to play video file on iPad using AVPlayerViewController

I have seen exactly the same issue here but its unanswered so asking the same question again. 我在这里看到了完全相同的问题,但未得到答复,因此再次提出相同的问题。 AVPlayer wont play video from url in iOS9 I tried all various ways to try to play the video but unfortunately they are not working for me. AVPlayer无法在iOS9中播放来自URL的视频,我尝试了各种方法来尝试播放视频,但不幸的是,它们不适用于我。 Here is the code : 这是代码:

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

and here is the screen shot of what it looks like currently by using above code. 这是使用上述代码的当前屏幕截图。 And I used another Apple code to check the video and it worked fine but the Apple code is like too much for me as its too complicated for simple things. 我使用了另一个Apple代码来检查视频,并且效果很好,但是Apple代码对我来说太过复杂了,因为对于简单的事情来说太复杂了。 Here it is : https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationSimplePlayer-iOS/ 它在这里: https : //developer.apple.com/library/prerelease/ios/samplecode/AVFoundationSimplePlayer-iOS/

在此处输入图片说明 Thanks AJ 谢谢AJ

I was finally able to figure this out by again looking into Apple's Sample code. 通过再次查看Apple的示例代码,我终于能够弄清楚这一点。 Solution: Added the AAPLPlayerView files to my project. 解决方案:将AAPLPlayerView文件添加到我的项目中。 Then changed my UIView in Storyboard to above class Then updated below lines of code into my view controller class: 然后将Storyboard中的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];
    }
}

And it worked. 而且有效。 The issue was the PlayerLayer was not set properly and the video was not visible due to that. 问题是PlayerLayer设置不正确,因此视频不可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM