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

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

问题描述

是否可以在Safari和/或UIWebView上自动播放youtube视频?我已经在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()。点击工作吗?

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];
}

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

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