簡體   English   中英

無法使用AVPlayerViewController在iPad上播放視頻文件

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

我在這里看到了完全相同的問題,但未得到答復,因此再次提出相同的問題。 AVPlayer無法在iOS9中播放來自URL的視頻,我嘗試了各種方法來嘗試播放視頻,但不幸的是,它們不適用於我。 這是代碼:

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/

在此處輸入圖片說明 謝謝AJ

通過再次查看Apple的示例代碼,我終於能夠弄清楚這一點。 解決方案:將AAPLPlayerView文件添加到我的項目中。 然后將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];
    }
}

而且有效。 問題是PlayerLayer設置不正確,因此視頻不可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM