如何在 AVPlayer IOS 中使用 URL 播放 youtube 视频?

use*_*231 7 youtube objective-c ios avplayer ios7

我想使用 YouTube URL 在 AVPlayer 中加载视频,但它没有显示任何内容。每当我使用 NSBundle 从本地存储加载时,它都可以正常工作。是否有其他加载视频的替代方法,或者我们可以在 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];
}
Run Code Online (Sandbox Code Playgroud)

dad*_*erk 2

您应该使用 iOS Youtube Helper 库来播放 YouTube 视频。

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

但我认为直接在播放器中使用 youtube 的网址不符合平台的 ToS。因此,如果您打算发布应用程序,我建议您使用 Youtube Helper Library。