How to embed Youtube video in my application?

I am trying to create a video player for my Swift application, but I keep getting the error "Unresolved AVPlayerViewController ID". What am I missing?

I am new to this; I may have to ask non-professionals several times. I scour the Internet, perhaps all day for a video on how to embed Youtube videos in my application without any results. If you could point me to a tutorial that would be awesome!

Thanks!

+11
ios swift swift2 video uiwebview
source share
6 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) ) } } 
+29
source share

Use YouTube-Player-iOS-Helper :

Step 1 : add the pod 'youtube-ios-player-helper', '0.1.4' to your Podfile and run pod install .

Step 2: Implement this code:

 import UIKit import youtube_ios_player_helper class ViewController: UIViewController, YTPlayerViewDelegate { @IBOutlet weak var playerView: YTPlayerView! override func viewDidLoad() { super.viewDidLoad() playerView.delegate = self let playerVars = ["playsinline": 1] // 0: will play video in fullscreen self.playerView.loadWithVideoId("youtubeId", playerVars: playerVars) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

Note. If you do not want to show playerVars video, set the rel key to 0 in the playerVars dictionary.

let playerVars = ["playsinline":1, "rel": 0 ]

+5
source share

Swift 3/4

You can play YouTube videos in AVPlayer with XCDYouTubeKit.

add

under 'XCDYouTubeKit'

into your project and write the code as shown 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 the video by replacing XCDYouTubeVideoQuality. HD720 .rawValue with medium360 or Small240

+2
source share

I did using the code below on the Swift 4+ version

 guard let url = URL(string: "<Your YuTube url>") else{ return } let safariViewControllerObject = SFSafariViewController(url: url) self.present(safariViewControllerObject, animated: true, completion: nil) 

Note: - I imported the SafariServices lib

+1
source share

Play YouTube videos in Swift 4.1

Add Pod pod 'XCDYouTubeKit'

import XCDYouTubeKit into ViewController

 func playYoutubeVideo(){ let strUrl = "https://www.youtube.com/watch?v=9m_K2Yg7wGQ" if let range = strUrl.range(of: "=") { let strIdentifier = strUrl.substring(from: range.upperBound) print("Identifier:\(strIdentifier)") let videoPlayerViewController = XCDYouTubeVideoPlayerViewController(videoIdentifier: strIdentifier) videoPlayerViewController.present(in: viewYTVideo) videoPlayerViewController.moviePlayer.play() } } 
0
source share

Swift 4 for iOS12, you must import the "WebKit" module

Make a socket for WKWebView. Use the following function that you must call in viewDidLoad ():

 func getVideo(videoCode: String) { let url = URL(string: "https://www.youtube.com/embed/\(videoCode)") myWKWebView.load(URLRequest(url: url!)) } 

For the videoCode argument in the getVideo function, you must use videoID, I will make it voluminous at the link below: https://www.youtube.com/watch?v= gI3pz7eFgfo & t = 838s

0
source share

All Articles