Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mp4 video in html5 video tag not playing in mobile chrome and mobile safari

I have this code to play a video in a html5 page:

  <video autoplay loop id="bgvid">
    <source src="video-background.mp4" poster="/poster.png" type="video/mp4">
  </video>

The problem is that it does not work in mobile chrome (Android Phone) and either in mobile safari (iPhone). But it works in "every" browser (tested with Safari, Chrome, Firefox) in desktop and also on mobile firefox (Android Phone).

How can I overcame this problem?

Edit: Added this code:

  var myVideo = document.getElementById("bgvid");

  function playVid() {
      myVideo.play();
  }

  function pauseVid() {
      myVideo.pause();
  }

If I add a button that trigger the function playVid() it works. So I think the problem is on the autoplay. I tried to trigger the function with the load event but it does not works.

like image 322
Mario Avatar asked Sep 03 '15 14:09

Mario


People also ask

Why can't I display a video in HTML5?

Your browser does not support HTML5 video. To show a video in HTML, use the <video> element: Your browser does not support the video tag. The controls attribute adds video controls, like play, pause, and volume. It is a good idea to always include width and height attributes.

Why can't I Play videos on Chrome mobile?

For instance, a change in the device's network settings could have blocked something, causing the video not to play in Chrome mobile. To fix this, you can go to your device's Settings > System > Reset and tap on the "Reset Network Settings" option. Confirm your choice and wait as the device's network settings would be set to their default value.

What is HTML5 video and how to use it?

It’s called HTML5 and more specifically for this post – HTML5 Video. For maximum browser support, use three types of video. Add them as <source> tags, wrap them all up in a video tag and you’re off and running. In its simplest form, a HTML5 video is implemented like so:

Does Safari support HTML5 video?

The video is working fine in Chrome and FireFox but not working at all neither in Safari on Desktop nor on iPhone or iPad , the output is simply a blank page that shows the controls of the the video tag but nothing is loaded Note that the Safari version that I have supports HTML5 video


1 Answers

Very simply there is no support for autoPlay on mobile safari . Please test all android browsers .

I use one trick (or show some popup to use event):

var ONLYONETIME_EXECUTE = null;
window.addEventListener('load', function(){ // on page load
 
      document.body.addEventListener('touchstart', function(e){
    
    if (ONLYONETIME_EXECUTE == null) {   

        video.play();

        //if you want to prepare more than one video/audios use this trick :             
          video2.play();
          video2.pause();
          // now video2 is buffering and you can play it programmability later 
          // My personal testing was maximum 6 video/audio for android 
          // and maybe 3 max for iOS using single click or touch.
          // Every next click also can prepare more audios/videos.

        ONLYONETIME_EXECUTE = 0;
    }

  }, false)
 
}, false)


// It is very usually that user touch screen  ...

Review :

I dont understand ios html5 politic . They stop supporting javascript console logger (i quest now :from ver 5.1 ios) .Auto play disabled , webrtc ... They wanna device who works perfect. Auto play can be dangerous on load. In near future i expect activation full html5 support on all mobile devices.

New update : I found this like positive answer :

Since the release of iOS 10 Apple has allowed muted video autoplay: https://webkit.org/blog/6784/new-video-policies-for-ios/

like image 54
Nikola Lukic Avatar answered Nov 02 '22 14:11

Nikola Lukic