Play a video from Youtube in a AVPlayerViewController in Swift [duplicate]

≡放荡痞女 提交于 2019-11-26 23:25:03

问题


This question already has an answer here:

  • How to embed a Youtube video into my app? 6 answers

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()

    }

}

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

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.

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).

Thanks for your help!


回答1:


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.



来源:https://stackoverflow.com/questions/34709880/play-a-video-from-youtube-in-a-avplayerviewcontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!