Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

enter image description here

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!

like image 390
Nick89 Avatar asked Jan 10 '16 19:01

Nick89


1 Answers

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.

like image 114
Marc Khadpe Avatar answered Nov 15 '22 20:11

Marc Khadpe