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
404 views
in Technique[技术] by (71.8m points)

Autostart html5 video using android 4 browser

I want to auto-start android html5 video using android 4 ice cream sandwich browser. I tried many java-script functions and autobuffer autoplay tags of html5 video. But nothing worked. I start android chrome client in webview via android app and that client should be able to auto-start video. When click the play button video plays but not auto play.

Is it restricted in android? Other thing to notice is that no call back methods are called in chromeClient even when we click the play button & video is playing & completed.

I have googled & found no positive result on this issue on Android 4.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

It seems that Android 4+ changed the requirements for the play() method to require user interaction. If you trigger play() from within a user event handler (eg. touchstart or mousedown), then you can play the video as long as you run it inside the same event loop.

This means that you shouldn't use async triggers to call play(), but rather call play inside the same event handler without setTimeout() and such, so stuff like time-delayed play is out of the question.

One way is to use the same trick on Android 4 as in iOS – use the first user interaction event to play() and pause() the video. This will enable the video for manipulation later, since you played it during a user initiated action. After you've successfully primed the video, you can call play methods at any time later, regardless of whether the call was made inside the event handler loop or not.

EDIT: Here's a sample code that works on HTC and Samsung, but not Galaxy Nexus 4.1 (requires user interaction to play):

var myVideo = document.getElementById('myvideo');

myVideo.addEventListener('canplay', function() {
  myVideo.play();
});

myVideo.load();
myVideo.play();

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

...