Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play YouTube content on tvOS

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 include a UIWebView so I cannot see a way to embed the YouTube video.

like image 270
Carl Thomas Avatar asked Sep 11 '15 17:09

Carl Thomas


People also ask

Can you play YouTube from Apple TV?

If you own a newer Apple TV, like the Apple TV HD (fourth-generation) and Apple TV 4K (fifth-generation), you have nothing to worry about. These both support the YouTube app because the models have a built-in App Store and run on tvOS.

How do I get YouTube on my Apple TV 2nd generation?

Answer: A: You cannot if it is a 2nd generation Apple TV, it is simply too old. However if you get a new Apple TV HD or Apple TV 4K you can download Youtube at the Apple TV App Store.

How do I get to my YouTube channel on Apple TV?

On your Apple TV, open the App Store app. Search for YouTube TV and click the search result. Click Get (and then Get again) to download and install the YouTube TV app on your Apple TV. When the app has downloaded and installed, click Open.


2 Answers

UIWebView and MPMoviePlayerController are not available for tvOS. Our next option is to use AVPlayer to play YouTube videos.

AVPlayer cannot play a YouTube video from a standard YouTube URL, ie. https://www.youtube.com/watch?v=8To-6VIJZRE. It needs a direct URL to the video file. Using HCYoutubeParser we can accomplish exactly that. Once we have the URL we need, we can play it with our AVPlayer like so:

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]; 

Note that this is NOT allowed under YouTube's TOS. Use it at your own risk. Your app may stop working at any point if YouTube notices you are not following the TOS or if YouTube changes the embed code it generates.

like image 64
Daniel Storm Avatar answered Sep 21 '22 18:09

Daniel Storm


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() 
like image 34
Burf2000 Avatar answered Sep 21 '22 18:09

Burf2000