Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed a Youtube video into my app?

I'm trying to create a video player for my Swift app, but I keep getting the error of 'unresolved identifier AVPlayerViewController'. What am I missing?

I'm a beginner at this, I may have to ask a few thousand times in layman's terms. I've been scouring the internet for perhaps a day now for a video for how to embed a Youtube video into my app, with no results. If you could point me over to a tutorial that would be awesome!

Thanks!

like image 508
Dandelyons Avatar asked Oct 16 '15 21:10

Dandelyons


People also ask

How do I embed a YouTube video in my phone?

Locate the video you wish to use and click on the Share link located beneath the video. 2. Next, click on the Embed icon The embed code will then be displayed. Copy and paste the embed code into your course.

Can you embed a video in an app?

Adding video to an Android app can be done just by adding a few lines of code. In this article, I will demonstrate how to embed a video to an Android app. This article will also explain how to control the video by playing, pausing and by seeking back and forth. This is really easy to do.


2 Answers

Xcode 8.2 • Swift 3.0.2

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID:String) {
        guard
            let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        wv.loadRequest( URLRequest(url: youtubeURL) )
    }
}

Xcode 7.3.1 • Swift 2.x

import UIKit

class ViewController: UIViewController {
    // create an outlet for your webview 
    @IBOutlet weak var wv: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // load your you tube video ID
        loadYoutube(videoID: "oCm_lnoVf08")
    }
    func loadYoutube(videoID videoID:String) {
        // create a custom youtubeURL with the video ID
        guard
            let youtubeURL = NSURL(string: "https://www.youtube.com/embed/\(videoID)")
            else { return }
        // load your web request
        wv.loadRequest( NSURLRequest(URL: youtubeURL) )
    }
}
like image 200
Leo Dabus Avatar answered Oct 19 '22 20:10

Leo Dabus


Swift 3/4

You can play youtube video in AVPlayer with the help of XCDYouTubeKit.

Add

pod 'XCDYouTubeKit'

into your project and write a code as below

func playVideo() {

        let playerViewController = AVPlayerViewController()
        self.present(playerViewController, animated: true, completion: nil)

        XCDYouTubeClient.default().getVideoWithIdentifier("KHIJmehK5OA") { (video: XCDYouTubeVideo?, error: Error?) in
            if let streamURL = video?.streamURLs[XCDYouTubeVideoQuality.HD720.rawValue] {
                playerViewController.player = AVPlayer(url: streamURL)
            } else {
                self.dismiss(animated: true, completion: nil)
            }
        }
    }

you can change the quality of video by replacing XCDYouTubeVideoQuality.HD720.rawValue with medium360 or with Small240

like image 10
Chetan Avatar answered Oct 19 '22 20:10

Chetan