Youtube player in swift

Following pod file is use for youtube player

pod ‘youtube-ios-player-helper’, ‘~> 0.1.6’

youtube player code

import UIKit
import youtube_ios_player_helper

class YoutubePlayerViewController: ViewController {
   
    var strVideoUrl: String = "https://www.youtube.com/watch?v=9s9FP0Cu8JY"
    @IBOutlet weak var playerView: YTPlayerView!
  
    override func viewDidLoad() {
        super.viewDidLoad()
        
        playerView.delegate = self
        let playerVars = ["playsinline": 1, "autohide": 1, "showinfo": 0, "controls":1, "origin" : "http://youtube.com"] as [String : Any] // 0: will play video in fullscreen
        let strYoutubeKey = extractYoutubeIdFromLink(link: strVideoUrl)
        if(strYoutubeKey != nil){
            self.playerView.load(withVideoId: strYoutubeKey!, playerVars: playerVars)
        }
    }
}

extension YoutubePlayerViewController: YTPlayerViewDelegate{
    func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
        //  print(state)
    }
    func playerView(_ playerView: YTPlayerView, didPlayTime playTime: Float) {
        //  print(playTime)
    }
    func playerViewDidBecomeReady(_ playerView: YTPlayerView) {
        // print(playerView)
    }
}
func extractYoutubeIdFromLink(link: String) -> String? {
    let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
    guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
        return nil
    }
    let nsLink = link as NSString
    let options = NSRegularExpression.MatchingOptions(rawValue: 0)
    let range = NSRange(location: 0, length: nsLink.length)
    let matches = regExp.matches(in: link as String, options:options, range:range)
    if let firstMatch = matches.first {
        return nsLink.substring(with: firstMatch.range)
    }
    return nil
}