5

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...

Jack L.
  • 1,257
  • 2
  • 17
  • 37
Piero
  • 9,173
  • 18
  • 90
  • 160

2 Answers2

4

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

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
nabouba
  • 49
  • 3
4

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.

Raphael Jeger
  • 5,024
  • 13
  • 48
  • 79