Youtube 视频在 iPhone 的 Safari 或 UIWebView 上自动播放 [英] Youtube video autoplay on iPhone's Safari or UIWebView

查看:17
本文介绍了Youtube 视频在 iPhone 的 Safari 或 UIWebView 上自动播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让 youtube 视频在 Safari 和/或 UIWebView 上自动播放?我在 iPhone 应用程序中看到过这种情况,tableview 显示没有 Youtube 预览图标的单元格(虽然很确定它是 UIWebView),当您点击单元格时它直接转到视频.

Is it possible to get a youtube video to autoplay on Safari and/or UIWebView? I've seen this done in an iPhone app, the tableview displays cells that do not have Youtube preview icon (pretty sure it's a UIWebView Though), when you tap the cell it directly goes to video.

这可以通过假装点击 youtube 视频来完成吗?如果是这样,怎么做?getElementById().click 会起作用吗?

Could this be done by faking a tap on the youtube video? If so, how? Would getElementById().click work?

推荐答案

诀窍是在 webview 中找到按钮.使用以下代码段.

the trick is to find the button in the webview. Use the following snippet.

- (void)loadWebUIViewWithYoutubeLink {
    webView.delegate = self;
    NSString *htmlString = [NSString stringWithFormat:[NSString
                             stringWithContentsOfFile:[[NSBundle mainBundle]
        pathForResource:@"YouTubeTemplate" ofType:@"txt"]],
                             @"b85hn8rJvgw",  @"b85hn8rJvgw", nil];
    [webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://youtube.com"]];
}

- (UIButton *)findButtonInView:(UIView *)view {
    UIButton *button = nil;

    if ([view isMemberOfClass:[UIButton class]]) {
        return (UIButton *)view;
    }

    if (view.subviews && [view.subviews count] > 0) {
        for (UIView *subview in view.subviews) {
            button = [self findButtonInView:subview];
            if (button) return button;
        }
    }

    return button;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    UIButton *b = [self findButtonInView:_webView];
    [b sendActionsForControlEvents:UIControlEventTouchUpInside];
}

这篇关于Youtube 视频在 iPhone 的 Safari 或 UIWebView 上自动播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆