Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed youtube video and autoplay on iOS 6 don't work [duplicate]

I'm trying to embed youtube video and autoplay it on my app. The code is not working on iOS6, however it runs on older iOS 5 perfectly.

I do it in this way:

-(IBAction)playVideo:(id)sender {

myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
myWebView.delegate = self;
[myWebView setAllowsInlineMediaPlayback:YES];
myWebView.mediaPlaybackRequiresUserAction=NO;

[myWebView loadHTMLString:[NSString stringWithFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>", @"http://www.youtube.com/watch?v=TbsXUJITa40"] baseURL:nil];

}

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

So- when the webview is loaded, it finds automatically the uibutton and the video starts. I can't understand, why in iOS 6 this method doesn't work anymore. It loads the video, but nothing appears...

Anyone can help me? I'm going crazy to try to solve it...

like image 942
Piero Avatar asked Sep 22 '12 20:09

Piero


2 Answers

You need to use JS command. This link should help.

like image 123
nabouba Avatar answered Oct 21 '22 10:10

nabouba


I hope it's not too late for an answer, but it plain simply is not possible in iOS. Like Apple states on it's documentation: https://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/AudioandVideoTagBasics/AudioandVideoTagBasics.html

To prevent unsolicited downloads over cellular networks at the user’s expense, embedded media cannot be played automatically in Safari on iOS—the user always initiates playback. A controller is automatically supplied on iPhone or iPod touch once playback in initiated, but for iPad you must either set the controls attribute or provide a controller using JavaScript.

Please pay attention that the documentation linked above is for Safari in general, not iOS-specific. To me, this was confusing on the first read until I noticed that.

So unfortunately no, for the moment this will not work on iOS.

like image 32
Raphael Jeger Avatar answered Oct 21 '22 11:10

Raphael Jeger