Video autoPlay is not working chrome and safari?

孤街醉人 提交于 2020-02-25 06:08:40

问题


I want to make the video autoplay without any user gesture in reactjs. I know as per recent google and apple web video policy we cannot autoplay a video having audio without user gesture.But i have seen few websites which still autoplays the video across modern web browsers also.

I came across many questions related to this issue on stackoverflow but none helped me.

Here is what i have tried.

Try 1.

<video id="miniVideo" preLoad="yes" autoPlay="autoplay" loop width="100%" height="auto" playsInline>
<source src="/mini/video/cooper.mp4" type="video/mp4" />
<source src="/mini/video/cooper.webm" type="video/webm" />
</video>

Try 2.

<iframe playsInline id="miniVideo" src="/mini/video/cooper.mp4" width="100%" 
 height="400px"
allow="autoplay; fullscreen"></iframe>

Try 3.

Script:

componentDidMount(){
    var videoTimer = document.getElementById("miniVideo");
    videoTimer.play();
}

HTML:

<video id="miniVideo" width="100%" height="100%">
<source src="/video/cooper.mp4" type="video/mp4" />
<p>This browser does not support the video element.</p>
</video>

Your help will be well appreciated.Thanks


回答1:


I am not sure about Safari but Chrome has changed the autoplay policy. Look here:https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

In order to autoplay, add muted attributed to video tag.
For example:

import React, { Component } from 'react';

class Player extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isVideoMuted: true
    };
  }

  handleMuteState = () => {
    this.setState(prevState => ({
      isVideoMuted: !prevState.isVideoMuted
    }));
  }

  render() {
    return (
      <div>
        <video muted={this.state.isVideoMuted} src="./video.mp4" />
        <button onClick={this.handleMuteState}>{this.state.isVideoMuted ? 'Unmute' : 'Mute'}</button >
      </div>
    );
  }
}

export default Player;


来源:https://stackoverflow.com/questions/52399034/video-autoplay-is-not-working-chrome-and-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!