How to play a youtube video in swift using AVPlayer?

13,149

Solution 1

The url you are using is not the url to the video, it is the url to a webpage which has the video embedded. There are tools out there that can help you get the actual url of the video file.

Solution 2

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. You can handle youtube videos in your own WebView.

You can use IFrame Player API.

Share:
13,149
Sukhdeep Singh Kalra
Author by

Sukhdeep Singh Kalra

I am a Mobile Application Developer dealing with native apps(android/ios) as well as Hybrid Apps(Phonegap/Titanium).

Updated on June 05, 2022

Comments

  • Sukhdeep Singh Kalra
    Sukhdeep Singh Kalra almost 2 years

    How to play a youtube video in swift using AVPlayer?

    I am using the below code to play a youtube video but nothing happens in avplayer. It remains blank.

    var urlstring = "https://www.youtube.com/watch?v=hMba_c-VSkI"
    var url = NSURL(string: urlstring)
    var av:AVPlayerViewController = segue.destinationViewController as AVPlayerViewController
    av.player = AVPlayer(URL: url)