YouTube 视频在 iOS6 上播放中断(在 iOS5 上工作正常) [英] YouTube video playback broken on iOS6 (works fine on iOS5)

查看:19
本文介绍了YouTube 视频在 iOS6 上播放中断(在 iOS5 上工作正常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一个按钮,按下该按钮后,您可以观看 YouTube 视频(电影预告片).在应用程序内,无需启动 safari.您可以在下面看到一个代码片段.这段代码在 iOS5 下工作得很好.但是,在 iOS 6 中, findButtonInView 中的 UIButton 始终为零.任何想法可能是什么原因?

In my app, I have a button which, when pressed, lets you watch a youtube video (a movie trailer). Within the app, without launching safari. Below you can see a code snippet. This code works pefrectly fine under iOS5. However, in iOS 6, the UIButton in findButtonInView is always nil. Any ideas what might be the reason?

youtubeWebView.delegate = self;
youtubeWebView.backgroundColor = [UIColor clearColor];

NSString* embedHTML = @" <html><head> <style type="text/css"> body {background-color: transparent;  color: white; }</style></head><body style="margin:0"><embed id="yt" src="%@?version=3&app=youtube_gdata" type="application/x-shockwave-flash"width="%0.0f" height="%0.0f"></embed></body></html>";

NSURL *movieTrailer;

if(tmdbMovie) movieTrailer = tmdbMovie.trailer;

else movieTrailer = [NSURL URLWithString:movie.trailerURLString];

NSString *html = [NSString stringWithFormat:embedHTML,
                                            movieTrailer,
                                            youtubeWebView.frame.size.width,
                                            youtubeWebView.frame.size.height];

[youtubeWebView loadHTMLString:html baseURL:nil];

[self addSubview:youtubeWebView];


- (void)webViewDidFinishLoad:(UIWebView *)_webView {

    isWatchTrailerBusy = NO;

    [manager displayNetworkActivityIndicator:NO];

    //stop the activity indicator and enable the button

     UIButton *b = [self findButtonInView:_webView];

    //TODO this returns null in case of iOS 6, redirect to the youtube app

    if(b == nil) {

        NSURL *movieTrailer;

        if(tmdbMovie) {
            movieTrailer = tmdbMovie.trailer;
        } else {
            movieTrailer = [NSURL URLWithString:movie.trailerURLString];
        }

        [[UIApplication sharedApplication] openURL:movieTrailer];

    } else {
        [b sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}


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

推荐答案

Apple 更改了 iOS6 中处理 YouTube 视频的方式.我还使用了 findButtonInView 方法,但它不再有效.

Apple changed how YouTube videos are handled in iOS6. I also was using the findButtonInView method but that no longer works.

我发现以下似乎有效(在 iOS < 6 中未经测试):

I've discovered the following seems to work (untested in iOS < 6):

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.webView.mediaPlaybackRequiresUserAction = NO;
}

- (void)playVideo
{
    self.autoPlay = YES;
    // Replace @"y8Kyi0WNg40" with your YouTube video id
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", @"http://www.youtube.com/embed/", @"y8Kyi0WNg40"]]]];
}

// UIWebView delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (self.autoPlay) {
        self.autoPlay = NO;
        [self clickVideo];
    }
}

- (void)clickVideo {
    [self.webView stringByEvaluatingJavaScriptFromString:@"
        function pollToPlay() {
            var vph5 = document.getElementById("video-player");
            if (vph5) {
                vph5.playVideo();
            } else {
                setTimeout(pollToPlay, 100);
            }
        }
        pollToPlay();
     "];
}

这篇关于YouTube 视频在 iOS6 上播放中断(在 iOS5 上工作正常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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