Using YTPlayerView to play YouTube embedded video in iOS failed with restriction error

I want to play the embedded YouTube video in an iOS application using the YTPlayerView provided at https://developers.google.com/youtube/v3/guides/ios_youtube_helper

When I tried to play this video with ID = "Ri7-vnrJD3k" ( https://www.youtube.com/embed/Ri7-vnrJD3k ), I received the error message "This video contains content from VEVO. It is limited to playback on certain sites. Watch YouTube. " Please note that there is no such problem when playing some other videos.

Is there a way to solve the above problem?

I could use an iframe to fully play the video using the quick sample code below the sample. But I donโ€™t know how to track when the user starts playing the video and when the video ends, since I want to do other user actions based on this data. If you know any solution, can you kindly inform me?

let frame = CGRectMake(0,0, self.view.frame.size.width, 240) playerView = UIWebView(frame: frame) playerView.allowsInlineMediaPlayback = true var embedHTML = NSString(format: "<html><head><style type=\"text/css\"> body { background-color: transparent; color: white; margin:0; width:100%%; height:100%% } </style> </head><body style=\"margin:0\"> <iframe width=100%% height=100%% src=\"%@?feature=player_detailpage&playsinline=1\" frameborder=\"0\" ></iframe> </body></html>", self.url.text) self.view.addSubview(playerView) playerView.loadHTMLString(embedHTML as String, baseURL: NSURL(string: "http://www.youtube.com")) 
+9
ios youtube youtube-api swift ytplayerview
source share
4 answers

By setting the origin property in my playerVars , I was able to play the embedded video.

 let playerVars = [ "playsinline" : 1, "showinfo" : 0, "rel" : 0, "modestbranding" : 1, "controls" : 1, "origin" : "https://www.example.com" ] 

Then call loadWithVideoId:: as usual.

YouTube

+23
source share
 NSDictionary *playerVars = @{ @"origin" : @"http://www.youtube.com", }; [self.playerView loadWithVideoId:@"videoId" playerVars:playerVars];` 

This version of Objective-C works for me.

+6
source share

YTPlayerView inturn uses an iframe to play YouTube videos. You cannot use iframes when playing copyrighted videos, you can use uiwebView to play this as an alternative

+1
source share

Swift 5.0

 @IBOutlet weak var videoPlayer: YTPlayerView! self.videoPlayer.load(withVideoId: "M7lc1UVf-VE", playerVars: ["origin": "http://www.youtube.com"]) 
0
source share

All Articles