How to autoplay HTML5 mp4 video on Android?

后端 未结 15 964
一向
一向 2020-11-27 05:53

I had developed a mobile page by asp.net to play mp4 video.

I know iOS had disabled the autoplay function to minimize user bandwidth, so how can i autoplay HTML5 mp

相关标签:
15条回答
  • 2020-11-27 06:13

    Autoplay only works the second time through. on android 4.1+ you have to have some kind of user event to get the first play() to work. Once that has happened then autostart works.

    This is so that the user is acknowledging that they are using bandwidth.

    There is another question that answers this . Autostart html5 video using android 4 browser

    0 讨论(0)
  • 2020-11-27 06:14

    I used the following code:

    // get the video
    var video = document.querySelector('video');
    // use the whole window and a *named function*
    window.addEventListener('touchstart', function videoStart() {
      video.play();
      console.log('first touch');
      // remove from the window and call the function we are removing
      this.removeEventListener('touchstart', videoStart);
    });
    

    There doesn't seem to be a way to auto-start anymore.

    This makes it so that the first time they touch the screen the video will play. It will also remove itself on first run so that you can avoid multiple listeners adding up.

    0 讨论(0)
  • 2020-11-27 06:16

    In Android 4.4 and above you can remove the need for a user gesture so long as the HTML5 Video component lives in your own WebView

    webview.setWebChromeClient(new WebChromeClient());
    webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
    

    To get the video to autoplay, you'd still need to add autoplay to the video element:

    <video id='video' controls autoplay>
      <source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' >
    </video>
    
    0 讨论(0)
  • 2020-11-27 06:16
    <video autoplay controls id='video1' width='100%' poster='images/top_icon.png' webkitEnterFullscreen poster preload='true'>
    <source src='http://192.xxx.xxx.xx/XXXXVM01.mp4' type='video/mp4; codecs='avc1.42E01E, mp4a.40.2' >
    </video>
    
    0 讨论(0)
  • 2020-11-27 06:18

    Can add muted tag.

    <video autoplay muted>
      <source src="video.webm" type="video/webm" />
      <source src="video.mp4" type="video/mp4" />
    </video>
    

    reference https://developers.google.com/web/updates/2016/07/autoplay

    0 讨论(0)
  • 2020-11-27 06:20

    similar to KNaito's answer, the following does the trick for me

    function simulateClick() {
      var event = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': true
      });
      var cb = document.getElementById('player'); 
      var canceled = !cb.dispatchEvent(event);
      if (canceled) {
        // A handler called preventDefault.
        alert("canceled");
      } else {
        // None of the handlers called preventDefault.
        alert("not canceled");
      }
    }
    
    0 讨论(0)
提交回复
热议问题