YouTube Embed player in Iframe doesn't work in

2019-04-09 07:56发布

I really need your help. Working on an iOS app. I want to play youtube. I understood from reading many many blogs and posts, that we need to use an iframe in order to play a youtube video.

However, on some videos I get: "This video contains content from XYZ. It is restricted from playback on certain sites. Watch on YouTube"

I read this question: Youtube in iOS5 - done button Tapped which gives the link to youtube api: https://developers.google.com/youtube/player_parameters They recommend to use the iframe.
the example for youtube site is:

<iframe id="ytplayer" type="text/html" width="640" height="390"
  src="http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com"
  frameborder="0"/>

The code I used:

<!DOCTYPE html>
<html>
<head>
<style>
* {
    border:0;
    margin:0;
    }
</style>
</head>
<body>
    <iframe webkit-playsinline id="player" type="text/html" width="320" height="180" src="http://www.youtube.com/embed/rEevIL1Wpcg?enablejsapi=1&playsinline=1&autoplay=1" frameborder="0">
    </iframe>
</body>
</html>

Can someone help me understand it? I check the embedded flag to be true, they are all clips that are allowed to play on mobile devices.

Example for videos that works on the device:

Example for videos that don't work on the device and bring up the error msg:

2条回答
beautiful°
2楼-- · 2019-04-09 08:23

you can use webview as youtube player

Try Below Code it is working for me

in .h file

@property (strong, nonatomic) UIWebView *webView;

and in your .m file

    NSString *videoURL = @"http://www.youtube.com/embed/M7lc1UVf-VE?autoplay=1&origin=http://example.com";

// if your url is not in embed format or it is dynamic then you have to convert it in embed format.

    videoURL = [videoURL stringByReplacingOccurrencesOfString:@"watch?v=" withString:@"embed/"];

    NSRange range = [videoURLString rangeOfString:@"&"];
    @try {
         videoURLString = [videoURLString substringToIndex:range.location];
    }
    @catch (NSException *exception) {

    }

    // here your link is converted in embed format.

    NSString* embedHTML = [NSString stringWithFormat:@"\
    <html><head>\
    <style type=\"text/css\">\
    iframe {position:absolute; top:50%%; margin-top:-130px;}\
    body {\
        background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <iframe width=\"100%%\" height=\"240px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
    </body></html>",videoURL];

    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:self.webView];
    [self.webView loadHTMLString:embedHTML baseURL:nil];

Here you can change webview frame as you want and also can change videoUrl.

查看更多
唯我独甜
3楼-- · 2019-04-09 08:27

There are two concepts, embeddable and syndicated. iOS devices use iframe so they basically embed. Android devices that use player API can check syndicated.

When you do a search->list, you can set videoEmbeddable and videoSyndicated to true.

Or if you are iterating through videos, for each video, you can do a video->list call with video id and check status.embeddable in the response.

Here is a blog post about this topic, even though examples are in v2, information is still relevant.

查看更多
登录 后发表回答