从我的 tvOS 应用程序播放 YouTube 视频 [英] Play YouTube videos from my tvOS application

查看:49
本文介绍了从我的 tvOS 应用程序播放 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 tvOS 应用程序播放 YouTube 视频.我发现通过 AVPlayer,但无法播放 YouTube 视频,因为 tvOS 不支持 UIWebView.我不想使用任何第三方库.

I want to play YouTube videos from my tvOS application. I found to play URL videos by AVPlayer, but not getting to play YouTube videos since UIWebView is not supported on tvOS. I don't want to use any third party libraries.

推荐答案

您需要使用 YoutubeSourceParserKit解析您从 url 收到的数据.然后您将获得网址,您将能够使用以下代码播放视频(适用于 tvOS 9 的 Swift 2 解决方案):

You need to use YoutubeSourceParserKit to parse data that you receive from url. Then you will have the url and you will be able to play video with the following code (Swift 2 solution for tvOS 9):

    Youtube.h264videosWithYoutubeURL(NSURL.init(string: "https://www.youtube.com/any-video-url")!) 
    { (videoInfo, error) -> Void in
        if let videoURLString = videoInfo?["url"] as? String,
            videoTitle = videoInfo?["title"] as? String {

            let videoURL = NSURL(string: videoURLString)
            let player = AVPlayer(URL: videoURL!)
            let playerViewController = AVPlayerViewController()
            playerViewController.player = player
            self.presentViewController(playerViewController, animated: true) {
                playerViewController.player!.play()
            }
        }
    }

这篇关于从我的 tvOS 应用程序播放 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆