如何在 tvOS 上播放 YouTube 内容

How to play YouTube content on tvOS

如何在 Apple tv 上播放 YouTube 视频OS?

YouTube 的嵌入功能在 iOS 9 中有效,因为 OS 有一个我们可以将其嵌入的 UIWebView。 新的 tvOS 不包含 UIWebView,因此我看不到嵌入 YouTube 视频的方法。

UIWebView and MPMoviePlayerController 不适用于 tvOS。我们的下一个选择是使用 AVPlayer 播放 YouTube 视频。

AVPlayer 无法播放来自标准 YouTube URL 的 YouTube 视频,即。 https://www.youtube.com/watch?v=8To-6VIJZRE。它需要直接 URL 到视频文件。使用 HCYoutubeParser 我们可以做到这一点。一旦我们有了我们需要的 URL,我们就可以像这样用我们的 AVPlayer 播放它:

NSString *youTubeString = @"https://www.youtube.com/watch?v=8To-6VIJZRE";
NSDictionary *videos = [HCYoutubeParser h264videosWithYoutubeURL:[NSURL URLWithString:youTubeString]];
NSString *urlString = [NSString stringWithFormat:@"%@", [videos objectForKey:@"medium"]];
AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:urlString]];

AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset];
AVPlayer *videoPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:videoPlayer];
avPlayerLayer.frame = playerView.layer.bounds;
[playerView.layer addSublayer:avPlayerLayer];

[videoPlayer play];

请注意, YouTube's TOS 不允许这样做。 使用它需要您自担风险。如果 YouTube 发现您没有遵守服务条款或 YouTube 更改了它生成的嵌入代码,您的应用可能会随时停止运行。

可悲的是,就连苹果员工也对此表示“否定”。来自 Apple Developer Forums thread on the topic:

There is no support for WebViews on tvOS, and so an iframe implementation will not work. TVML does not offer this capability.

Swift 2.0版大牛风暴的回答:

let youTubeString : String = "https://www.youtube.com/watch?v=8To-6VIJZRE"
let videos : NSDictionary = HCYoutubeParser.h264videosWithYoutubeURL(NSURL(string: youTubeString))
let urlString : String = videos["medium"] as! String
let asset = AVAsset(URL: NSURL(string: urlString)!)

let avPlayerItem = AVPlayerItem(asset:asset)
let avPlayer = AVPlayer(playerItem: avPlayerItem)
let avPlayerLayer  = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height);
self.view.layer.addSublayer(avPlayerLayer)
avPlayer.play()

作为 Daniel Storm 答案的补充,您还可以使用以下代码通过新的 TVML 语言实现此目的:

<lockup videoURL="http://mp4-url.mp4">
<img src="video thumbnail.jpg" />
</lockup>

(为此,您需要直接视频文件 (mp4),这也是 Youtube TOS 不允许的)

编辑,还找到了 .JS 解决方案:

我用了这个https://cocoapods.org/pods/youtube-parser

Youtube.h264videosWithYoutubeURL(testURL) { (videoInfo, error) -> Void in
    if let videoURLString = videoInfo?["url"] as? String,
         videoTitle = videoInfo?["title"] as? String {
             print("\(videoTitle)")
             print("\(videoURLString)")
             self.playVideo(videoURLString)
    }
}

XCDYouTubeKit 一段时间以来一直是直接在 iOS 上播放 YouTube 视频的流行方式,开发者最近对其进行了更新以支持新的 AppleTV。

这是来自 Objective-C example 的代码:

AVPlayerViewController *playerViewController = [AVPlayerViewController new];
[self presentViewController:playerViewController animated:YES completion:nil];

__weak AVPlayerViewController *weakPlayerViewController = playerViewController;
[[XCDYouTubeClient defaultClient] getVideoWithIdentifier:playlistItem.snippet.resourceId.videoId completionHandler:^(XCDYouTubeVideo * _Nullable video, NSError * _Nullable error) {
    if (video)
    {
        NSDictionary *streamURLs = video.streamURLs;
        NSURL *streamURL = streamURLs[XCDYouTubeVideoQualityHTTPLiveStreaming] ?: streamURLs[@(XCDYouTubeVideoQualityHD720)] ?: streamURLs[@(XCDYouTubeVideoQualityMedium360)] ?: streamURLs[@(XCDYouTubeVideoQualitySmall240)];
        weakPlayerViewController.player = [AVPlayer playerWithURL:streamURL];
        [weakPlayerViewController.player play];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}];

Swift example here.

经过一些尝试,我很高兴地宣布:

youtube://watch/video_id

将打开 YouTube 应用并播放 YouTube 视频。

尽情享受吧。