使用 android 4 浏览器自动启动 html5 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11758651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:45:17  来源:igfitidea点击:

Autostart html5 video using android 4 browser

androidhtml5-videoandroid-4.0-ice-cream-sandwichandroid-4.4-kitkatandroid-browser

提问by M P Mathugama

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.

我想使用 android 4 冰淇淋三明治浏览器自动启动 android html5 视频。我尝试了许多 java 脚本函数和 html5 视频的自动缓冲自动播放标签。但没有任何效果。我通过 android 应用程序在 webview 中启动 android chrome 客户端,该客户端应该能够自动启动视频。单击播放按钮时,视频播放但不自动播放。

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.

安卓有限制吗?需要注意的另一件事是,即使我们单击播放按钮并且视频正在播放和完成,chromeClient 中也不会调用任何回调方法。

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

我在 Android 4 上用谷歌搜索并没有发现关于这个问题的积极结果。

回答by Klemen Slavi?

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. touchstartor mousedown), then you can play the video as long as you run it inside the same event loop.

似乎 Android 4+ 更改了该play()方法的要求,以要求用户交互。如果您play()从用户事件处理程序(例如touchstartmousedown)中触发,那么只要您在同一事件循环中运行该视频,就可以播放该视频。

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.

这意味着您不应该使用异步触发器来调用play(),而应该在没有setTimeout()诸如此类的情况下在同一个事件处理程序中调用 play ,因此像延时播放这样的东西是不可能的。

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.

一种方法是在 Android 4 上使用与在 iOS 中相同的技巧——使用第一个用户交互事件play()pause()视频。这将使视频能够在以后进行操作,因为您是在用户启动的操作期间播放的。成功启动视频后,您可以在以后随时调用播放方法,无论调用是否在事件处理程序循环内进行。

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

编辑:这是一个适用于 HTC 和三星的示例代码,但不适用于 Galaxy Nexus 4.1(需要用户交互才能播放):

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

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

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

回答by brendan

Android actually has an API for this! The method is setMediaPlaybackRequiresUserGesture(). I found it after a lot of digging into video autoplay and a lot of attempted hacks from SO. Here's an example from blair vanderhoof:

Android 实际上有一个用于此的 API!该方法是 setMediaPlaybackRequiresUserGesture()。我在对视频自动播放进行了大量挖掘并尝试了很多来自 SO 的尝试后找到了它。这是布莱尔范德霍夫的一个例子:

package com.example.myProject;

import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings;

public class myProject extends CordovaActivity 
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html");

        WebSettings ws = super.appView.getSettings();
        ws.setMediaPlaybackRequiresUserGesture(false);
    }
}

works on Android 4.4.4

适用于 Android 4.4.4