9

I want play embedded YouTube video in iOS app using 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 got the error message "This video contains content from VEVO. It is restricted from playback on certain sites. Watch on YouTube". Note that there is no such issue when playing some other videos.

Is there any way to address the above issue?

I could use iframe to play the video inline successfully with below sample swift code. But I don't know how to track when user starts to play the video and when the video completes since I want to do other custom action based on those information. If you know any solution, could you kindly let me know?

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"))
JAL
  • 41,701
  • 23
  • 172
  • 300
Kevin Su
  • 93
  • 1
  • 3

4 Answers4

26

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 you normally would.

YouTube

JAL
  • 41,701
  • 23
  • 172
  • 300
  • Do you need to set the origin to a generic url or do you need to set the origin to the url of the copyrighted material? If so, how do you fetch the copyrighted material's url? Image many different videos playing in the playerView. What do you do? – Eric Nov 05 '15 at 05:09
  • 1
    @Eric Read up on the `origin` property in the API docs: https://developers.google.com/youtube/player_parameters?hl=en. It has nothing to do with the video's URL, it's a security measure for the IFrame API. – JAL Nov 05 '15 at 17:27
  • I encountered the same issue but your solution seems not working for me. – Nesh Nov 12 '15 at 09:19
  • @Nesh what video id are you trying to play? Is it a Vevo video? – JAL Nov 12 '15 at 14:27
  • 1
    I tried to play a video of a TV network also i tried playing the id you have mentioned too, but no luck – Nesh Nov 13 '15 at 11:01
  • You sir are my hero of the day! – Droid Chris Feb 07 '17 at 04:42
  • From the doc link above: If you are using the IFrame API ... you should always specify your domain as the origin parameter value. (so it should be `https://yourappnamehere.com` for example, not `https://youtube.com`) – Jordan H Sep 30 '20 at 23:05
8
NSDictionary *playerVars = @{
                             @"origin" : @"http://www.youtube.com",
                             };
[self.playerView loadWithVideoId:@"videoId" playerVars:playerVars];`

This Objective-C version works for me.

JAL
  • 41,701
  • 23
  • 172
  • 300
Mayuri R Talaviya
  • 613
  • 1
  • 9
  • 14
1

YTPlayerView inturn uses iframes to play youtube video. You cannot use iframe in playing copyrighted youtube videos you can use a uiwebView to play this as an alternative

Akshay Karanth
  • 347
  • 1
  • 13
  • 1
    This is incorrect. The YTPlayerView is just a wrapper around a UIWebView. – JAL Jul 06 '15 at 13:13
  • 1
    @JAL No playing a iframe video in UIWebview also does not work for copyrighted videos. Ex:https://www.youtube.com/embed/-cUSV8MCOLY?autoplay=1, this is a copyrighted video try and paste this on ur browser. http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1 This is a normal video paste this link in your browser, this will play as this is not copyrighted.......Paste the link don't click on it, it will redirect you to youtube page otherwise – Akshay Karanth Aug 14 '15 at 03:42
  • 3
    An author can restrict their content to not play on mobile devices or prevent embedding in external websites, this is not, however, the default behaviour for all works that are copyrighted. (Obviously, as all works are copyrighted automatically unless the author of that work has explicitly placed that work in the public domain) – James Wakefield Dec 15 '15 at 05:28
1

Swift 5.0

@IBOutlet weak var videoPlayer: YTPlayerView!

 self.videoPlayer.load(withVideoId: "M7lc1UVf-VE", playerVars: ["origin": "http://www.youtube.com"])
Juanes30
  • 2,398
  • 2
  • 24
  • 38