gpt4 book ai didi

ios - 将 AVPlayerLayer 添加到 UIViewController

转载 作者:行者123 更新时间:2023-11-28 23:38:24 24 4
gpt4 key购买 nike

是否可以在 UIViewController 中显示 AVPlayerLayer,稍后在 UIView 中显示。

我想要做的示例代码:

...
UIView *parentView = reinterpret_cast<UIView *>(window->winId());

AVPlayer *_player;
AVURLAsset *_asset;
AVPlayerItem *_playerItem;
AVPlayerLayer *m_playerLayer;
_player = [[AVPlayer alloc] init];
NSURL *baseURL = [[NSURL alloc] initWithString: @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
_asset = [AVURLAsset assetWithURL:baseURL];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
m_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];

// works with AVPlayerViewController
// AVPlayerViewController *playerController = [[AVPlayerViewController alloc] init];
// playerController.player = _player;

UIViewController *viewController = [[UIViewController alloc] init];
[viewController.view.layer addSublayer:m_playerLayer];

viewController.view.frame = CGRectMake(this->x(), this->y(), this->width(), this->height());
[parentView addSubview:viewController.view];

[_player play];
...

如果我尝试将播放器添加到 AVPlayerViewController 然后显示它,它会起作用,但我只想使用 AVPlayerLayer,因为我不需要视频控件,因为我将单独实现该功能.

最佳答案

你可以使用AVPlayerViewController

avController.showsPlaybackControls = false;

或者直接添加层

_player = [[AVPlayer alloc] init];
NSURL *baseURL = [[NSURL alloc] initWithString: @"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
_asset = [AVURLAsset assetWithURL:baseURL];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
[_player replaceCurrentItemWithPlayerItem:_playerItem];
m_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
m_playerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:m_playerLayer ];
[_player play];

enter image description here

关于ios - 将 AVPlayerLayer 添加到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54239257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com