0

我在这里看到了完全相同的问题,但没有得到答案,所以再次问同样的问题。 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/

在此处输入图像描述 谢谢阿杰

4

1 回答 1

0

通过再次查看 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 设置不正确,因此视频不可见。

于 2015-11-19T19:18:56.570 回答