Jak odtwarzać treści z YouTube na tvOS

Jak można odtworzyć film z YouTube na Apple tvOS?

Funkcja osadzania YouTube działa w systemie iOS 9, ponieważ system operacyjny ma UIWebView, w którym możemy go osadzić. Nowy tvOS nie zawiera UIWebView, więc nie widzę sposobu, aby osadzić film z YouTube.

Author: Joel Farris, 2015-09-11

6 answers

UIWebView oraz MPMoviePlayerController nie są dostępne dla tvOS. Naszą następną opcją jest użycie AVPlayer do odtwarzania filmów na YouTube.

AVPlayer nie można odtworzyć filmu YouTube ze standardowego adresu URL YouTube, np. https://www.youtube.com/watch?v=8To-6VIJZRE. Potrzebuje bezpośredniego adresu URL do pliku wideo. Używając HCYoutubeParser możemy to osiągnąć. Gdy mamy adres URL, którego potrzebujemy, możemy go odtworzyć za pomocą naszej AVPlayer w następujący sposób:

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

Zauważ, że jest tonie dozwolone PodTOS YouTube . użyj go na własne ryzyko . Twoja aplikacja może przestać działać w dowolnym momencie, jeśli YouTube zauważy, że nie przestrzegasz TOS lub jeśli YouTube zmieni wygenerowany kod osadzania.

 43
Author: Daniel Storm,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-15 02:50:30

Swift 2.0 Wersja odpowiedzi Daniela STORMA:

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()
 17
Author: Burf2000,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-22 12:17:10

XCDYouTubeKit jest popularnym sposobem na bezpośrednie odtwarzanie wideo z YouTube na iOS przez jakiś czas, a deweloper niedawno zaktualizował go, aby obsługiwać nowy AppleTV.

Oto kod z przykładu Objective - C :

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

Szybki przykład tutaj.

 6
Author: Nick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-15 03:07:18

Niestety, nawet osoba z personelu Apple odbiła się echem "negatywności" w tej sprawie. Z wątku Forum programistów Apple na temat:

Nie ma wsparcia dla Webviewów na tvOS, więc implementacja iframe nie będzie działać. TVML nie oferuje tej możliwości.

 5
Author: Joel Farris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-21 15:35:32

Jako dodatek do odpowiedzi Daniela STORMA, możesz to również osiągnąć za pomocą nowego języka TVML za pomocą kodu:

<lockup videoURL="http://mp4-url.mp4">
<img src="video thumbnail.jpg" />
</lockup>
Aby to zadziałało, potrzebujesz bezpośredniego pliku wideo (mp4), który również nie jest dozwolony przez TOS Youtube)

EDIT, znaleziono również .Rozwiązanie JS: Jak odtworzyć wideo na tvOS Dla Apple TV?

 1
Author: Bldjef,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:24

Użyłem tego 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
Author: pillade,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-24 01:59:02