How to play YouTube content on tvOS

前端 未结 7 1506
北恋
北恋 2020-11-29 21:36

How would one play a YouTube video on Apple tvOS?

YouTube\'s embed feature works in iOS 9 as the OS has a UIWebView we can embed it into. The new tvOS does not incl

相关标签:
7条回答
  • 2020-11-29 21:59

    I used this one 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)
        }
    }
    
    0 讨论(0)
  • 2020-11-29 22:04

    XCDYouTubeKit has been a popular way to directly play a YouTube video on iOS for a while, and the developer recently updated it to support the new AppleTV.

    Here's the code from the 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.

    0 讨论(0)
  • 2020-11-29 22:05

    As an addition to Daniel Storm's answer, you can also achieve this with the new TVML language using the code:

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

    (for this to work you'll need the direct video file (mp4) which also is not allowed by the Youtube TOS)

    EDIT, found also the .JS solution: How do I play a video on tvOS for Apple TV?

    0 讨论(0)
  • 2020-11-29 22:23

    Swift 2.0 version of Daniel Storm's answer:

    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()
    
    0 讨论(0)
  • 2020-11-29 22:23

    Sadly, even an Apple Staff person echoed the “negative” on this. From an 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.

    0 讨论(0)
  • 2020-11-29 22:23

    After some trying I'm happy to announce that:

    youtube://watch/video_id
    

    will open the YouTube app and play the YouTube video.

    Enjoy.

    0 讨论(0)
提交回复
热议问题