AVPlayer doesn't show anything

亡梦爱人 提交于 2019-11-30 19:55:37

You'd have to do the following:

AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:item.url]];
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

avPlayerLayer.frame = self.frame;
[containerView.layer addSublayer:avPlayerLayer];
[self addSubview:containerView];
[avPlayer play];

Hope this helps.

Addendum:

It also depends on your URL; if as you said, you have one such as in this format:

http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player

Then you should use this instead:

[NSURL urlWithString:item.url]; 

Given that item is your object and url is a property there of object type NSString.

Use the requestPlayerItemForVideo method of PHImageManager to acquire an AVPlayerItem; it is the simplest, sure-fire way to play an AVAsset, performing flawlessly and consistently.

I use it here:

https://youtu.be/7QlaO7WxjGg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!