如何在AVPlayer IOS中使用URL播放youtube视频? [英] How to play youtube video using URL in AVPlayer IOS?
问题描述
我想使用YouTube网址在AVPlayer中加载视频,但它没有显示任何内容。无论何时我使用NSBundle从本地存储加载它都运行正常。有加载视频的替代方法或我们可以在AVPlayer中执行某些操作。
I want to load video in AVPlayer using YouTube URL but it is not showing anything.Whenever i am loading from a local storage using NSBundle it is working fine.Is there is any alternative to load video or we can do something in AVPlayer.
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryError];
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:@"http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player"]];
avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset];
self.songPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer: self.songPlayer];
self.avPlayerLayer.frame = self.view.layer.bounds;
UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
[newView.layer addSublayer:avPlayerLayer];
[self.view addSubview:newView];
[ self.songPlayer play];
}
推荐答案
你应该使用iOS Youtube用于播放YouTube视频的帮助程序库。
You should use the iOS Youtube Helper library for playing youtube videos.
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
我不知道您是否可以使用AVPlayer。我在CocoaControls上看过一些使用MPMoviePlayerController的例子,如下所示: https://www.cocoacontrols.com/ controls / hcyoutubeparser 或者这个: https://www.cocoacontrols.com/controls/xcdyoutubevideoplayerviewcontroller
I don't know if you can use the AVPlayer. I've seen some examples using MPMoviePlayerController on CocoaControls, like this one: https://www.cocoacontrols.com/controls/hcyoutubeparser or this one: https://www.cocoacontrols.com/controls/xcdyoutubevideoplayerviewcontroller
但我不认为直接在你的播放器中使用youtube的url符合平台的ToS。因此,如果您打算发布应用,我建议您使用Youtube Helper Library。
But I don't think using youtube's url directly in your player fits the ToS of the platform. So I will recommend you tu use the Youtube Helper Library if you are planning to publish your app.
这篇关于如何在AVPlayer IOS中使用URL播放youtube视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!