在Swift的AVPlayerViewController中播放来自Youtube的视频 [英] Play a video from Youtube in a AVPlayerViewController in Swift

查看:139
本文介绍了在Swift的AVPlayerViewController中播放来自Youtube的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想在我的应用中播放电影预告片。用户将按下按钮,然后播放视频。我已将 import AVKit 导入AVFoundation 行添加到我的文件中。这是我到目前为止播放视频的代码:

Okay so I'm looking to play film trailers in my app. The user will press a button, and then it plays the video. I have added the import AVKit and import AVFoundation lines to my file. This is the code I have so far for making the video play:

@IBAction func playTrailerPressed(sender: AnyObject) {

    let videoURL = NSURL(string: "https://youtu.be/d88APYIGkjk")
    let player = AVPlayer(URL: videoURL!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.presentViewController(playerViewController, animated: true) {

        playerViewController.player!.play()

    }

}

这似乎启动 AVPlayerViewController ,但不播放YouTube上的视频。相反,我得到以下内容:

This seems to launch an AVPlayerViewController, but doesn't play the video from YouTube. Instead, I get the below:

< img src =https://i.stack.imgur.com/oiws4.pngalt =在此输入图像说明>

我有尝试过来自YouTube的共享和嵌入链接,但都不起作用。如果我使用最后有视频文件名的链接,它会正常播放,例如:https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 - 所以我知道代码有效。

I have tried both the sharing and embedding link from YouTube, but neither work. If I use a link which has the video file name at the end, it plays it fine, for example: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"- So I know the code works.

有人知道这种方式是否可以通过YouTube视频使用? (我也尝试过IMDb和Apple的预告片,但它也是一样的。)

Does anyone know if this is possible to use this way with a YouTube video? (I have also tried trailers from IMDb and Apple, but it's the same).

感谢您的帮助!

推荐答案

AVPlayer 仅播放电影文件,YouTube视频不会直接在其网址上公开为电影文件。看起来处理YouTube视频的首选方法是在您的应用中嵌入网络视图。有关如何执行此操作的信息,请参阅此页

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. It looks like the preferred way to handle YouTube videos is to embed a web view into your app. See this page for information from Google on how to do that.

这篇关于在Swift的AVPlayerViewController中播放来自Youtube的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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