用Swift播放Youtube视频

时间:2015-06-05 13:20:14

标签: swift

嘿我想在我的应用中显示YouTube视频,但如果我运行它只有一个黑屏??

func playVideo() {
    var audioplayer : MPMoviePlayerController!

    var url:NSURL = NSURL(string: "https://youtu.be/7n1KPclvGQY.mp4")!

    var MPMoviePlayerViewController = MPMoviePlayerController(contentURL: url)

    MPMoviePlayerViewController.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
    MPMoviePlayerViewController.movieSourceType = MPMovieSourceType.File

    self.view.addSubview(MPMoviePlayerViewController.view)
    MPMoviePlayerViewController.prepareToPlay()
    MPMoviePlayerViewController.play()
    MPMoviePlayerViewController.pause()
    }

2 个答案:

答案 0 :(得分:0)

没有非常好的方法来做到这一点。有this library但它打破了YouTube的服务条款。

最好拨打[UIApplication sharedApplication] openURL:...],如果已安装,则会在Safari或YouTube应用中打开。

如果您拥有自己的内容,也许可以尝试自己托管并自己使用相同的方法?

答案 1 :(得分:0)

使用自定义库播放YouTube视频 使用自定义https://github.com/larcus94/LBYouTubeView

它是MPMoviePlayerViewController的子类。

LBYouTubeView只是一个能够在MPMoviePlayerController中显示YouTube视频的小视图。您甚至可以选择高质量和标准质量的流。

它只会加载YouTube移动网站的HTML代码,并在脚本代码中查找数据。

LBYouTubeView不使用UIWebView,使其更快,更清晰。

相关问题