Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

ios - Youtube video autoplay inside UIWebview

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved the problem by adding the follow code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...