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

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

问题描述

好的,我想在我的应用中播放电影预告片.用户将按下一个按钮,然后播放视频.我已将 import AVKitimport 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:

我尝试了 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 视频不会直接在其 URL 处作为电影文件公开.看起来处理 YouTube 视频的首选方式是将 Web 视图嵌入到您的应用程序中.请参阅此页面,了解 Google 提供的有关如何执行此操作的信息.

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天全站免登陆