【问题标题】:How to launch youtube video in browser without tapping on the play icon如何在不点击播放图标的情况下在浏览器中启动 youtube 视频
【发布时间】:2013-01-04 07:22:27
【问题描述】:

我正在使用下面的代码来播放 youtube url:

NSString *youtubeUrl = [NSString stringWithFormat:@"http://www.youtube.com/v/%@",[photo videoUrl]];
[self embedYouTube:[youtubeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] frame:CGRectMake(10, 120, 300, 200)];

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame
{
    NSString* embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
        background-color: black;\
    color: black;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
    NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
    if(videoView == nil) {
        videoView = [[UIWebView alloc] initWithFrame:frame];
        videoView.layer.borderColor = [[UIColor blackColor] CGColor];
        [self.view addSubview:videoView];
        [videoView setHidden:NO];
    }
    [videoView loadHTMLString:html baseURL:nil];
}

用户需要点击 web 视图来播放此视频。不点击播放图标就不能直接加载视频吗?

【问题讨论】:

    标签: iphone ios ipad ios6 youtube


    【解决方案1】:

    我认为简短的回答是,如果您使用 webView 显示视频,则 Apple 指南不再允许自动播放。 docs specifically state

    警告:为了防止在移动网络上未经请求的下载 用户的费用,嵌入式媒体不能自动播放 iOS 上的 Safari — 用户始终启动播放。

    Google Developer / YouTube API docs 引用了相同的文档。

    【讨论】:

      【解决方案2】:

      您应该检查LBYouTubePlayerController,它使用 MoviePlayer 组件来加载 youtube 视频,并且自定义内容要容易得多,例如自动播放功能。

      【讨论】:

        【解决方案3】:

        勾选这种方式来嵌入你的播放,它支持自动播放

        https://developers.google.com/youtube/player_parameters

        【讨论】:

        • 我认为这在问题范围内是不允许的(即移动 Safari)。
        【解决方案4】:

        在 URL 中添加参数autoplay=1,如下所示:

        NSString *youtubeUrl = [NSString stringWithFormat:@"http://www.youtube.com/v/%@?autoplay=1",[photo videoUrl]];
        

        【讨论】:

        • 如果我包含“?”,Webview 将无法解析在链接中,所以我用斜杠更新了链接:youtube.com/v/%@/autoplay/1 ------ 但它没有播放。我该如何解决这个问题?
        • 在将?autoplay=1 添加到帖子结果后,您能NSLog 的值youtubeUrl 吗?
        • 在浏览器中它可以正常播放,如果我给这个链接youtube.com/v/_6wiHPxa-_U?autoplay=1 ----- 当我在 iPhone 中测试应用程序时它没有打开视频。它仍然需要点击播放按钮。
        • 这里是html字符串: youtube.com/v/_6wiHPxa-_U?autoplay=1" type="application/x-shockwave-flash" width="300" height ="200">
        • 你在使用 iOS6 吗?我读到 Apple 改变了处理 youtube 视频的方式并且自动播放不起作用,请尝试其他答案或查看此other question中的答案
        猜你喜欢
        • 2021-04-16
        • 2018-03-12
        • 2018-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 2015-12-22
        相关资源
        最近更新 更多