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

拜拜、爱过 提交于 2019-11-28 03:34:47

问题


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"))

回答1:


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.




回答2:


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

This Objective-C version works for me.




回答3:


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




回答4:


Swift 5.0

@IBOutlet weak var videoPlayer: YTPlayerView!

 self.videoPlayer.load(withVideoId: "M7lc1UVf-VE", playerVars: ["origin": "http://www.youtube.com"])


来源:https://stackoverflow.com/questions/30972123/using-ytplayerview-to-play-embedded-youtube-video-in-ios-failed-with-restriction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!