5

I found many questions in this forum regarding embedding youtube video and autoplay, but none of them cleared all my doubts.

I found two methods to embed youtube video in UIWebView

1)

NSString *youTubeHTMLTemplate = @"<html><head><style type=\"text/css\">body { background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>"; 
finalHtml = [NSString stringWithFormat:youTubeHTMLTemplate, fullYopuTubeUrl, htmlFrameWidth, htmlFrameHeight];

2)

NSString *youTubeHTMLTemplate = @"<html><body style=\"margin:0;padding:0;\"><iframe class=\"youtube-player\" type=\"text/html\" width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
finalHtml = [NSString stringWithFormat:youTubeHTMLTemplate, htmlFrameWidth, htmlFrameHeight, videoID];

If I use (1) method and follow the approach given here my video autoplays. Idea is to find button in UIWebView and send touch up event to it.

If I use (2) and follow the same approach video does not autoplay.

I think that the (2) approach is recommended as the (1) is for Flash Videos only (Right?).

Is by any means (2) is possible ? This link says NO.

Can anyone confirm

  • If I am correct that (2) is recommended
  • Autoplay with (2) is not possible.
Community
  • 1
  • 1
msk
  • 8,635
  • 5
  • 39
  • 70
  • "if I use (1) method and follow the approach given here".. you are linking to the question and not a specific answer on that page.. there are like 10 answers.. can you please link to the specific answer you had in mind? – abbood Dec 03 '13 at 06:46
  • sorry @abbood but never mind the approach i linked will no longer work on iOS > 5 (or 6). Use YouTube's JS APIs. – msk Dec 03 '13 at 14:28
  • hum.. i was hoping for a more generic solution.. for example i'm linking to livestream flash video and i would like it to play immediately as soon as the user loads the viewController containing the webview with the livestream video code.. i think what i'm looking for is automating the user's click on the play button.. but then again i donno if apple would accept such code – abbood Dec 04 '13 at 07:41
  • this [post](http://stackoverflow.com/questions/3128053/simulate-a-mouse-click-in-flash-using-javascript) discounts the possibility of clicking on a flash video using javascript.. bummer! – abbood Dec 04 '13 at 07:45

4 Answers4

4

I solved the problem by adding the follow code:

webview.mediaPlaybackRequiresUserAction=NO;
webview.allowsInlineMediaPlayback=YES;
3

Q. If I am correct that (2) is recommended

A. Yes (2) method is for i-devices where no flash support.

Q. Autoplay with (2) is not possible.

A. No autoplay is not possible as of now.

Update: I am able to autoplay youtube video by using Youtube JS APIs see https://developers.google.com/youtube/js_api_reference

Just call playVideo from onPlayerReady

function onPlayerReady(event) {
                event.target.playVideo();                    
            }
msk
  • 8,635
  • 5
  • 39
  • 70
0

Autoplay is possible by issuing:

webView.mediaPlaybackRequiresUserAction=FALSE;
Pedro Rolo
  • 23,843
  • 11
  • 53
  • 92
  • This is old thread, but I remember setting mediaPlaybackRequiresUserAction = NO alone doesn't helped me. I updated my answer above and I am able to autoplay youtube video. – msk Dec 11 '12 at 16:37
  • oh may be yes because last I tried it on iOS5 where it did not worked. Though approach in my answer is working for iOS>=5. – msk Dec 12 '12 at 16:37
  • i've been bashing my head all day because of this line. Thanks! – dreampowder May 11 '13 at 15:58
-1

You can also use this trick from the Saurabh Wadhwa Answer that fire the endActionsForControlEvents:UIControlEventTouchUpInside after UIWebView finished download

Community
  • 1
  • 1
Sakares
  • 596
  • 11
  • 31