本文介绍了视频自动播放功能无法在Chrome和Safari浏览器中正常运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有任何用户手势的情况下使视频自动播放.我知道,根据最近的Google和Apple网络视频政策,我们不能在没有用户手势的情况下自动播放具有音频的视频.但是,我看到的网站仍然可以在现代网络浏览器中自动播放视频.

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.

我在stackoverflow上遇到了许多与此问题相关的问题,但没有一个帮助我.

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

这是我尝试过的.

尝试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>

尝试2.

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

尝试3.

脚本:

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

推荐答案

我不确定Safari,但Chrome更改了自动播放政策.在这里查看: https://developers.google.com/web /updates/2017/09/autoplay-policy-changes

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

为了自动播放,请添加归属于video标签的muted.
例如:

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;

这篇关于视频自动播放功能无法在Chrome和Safari浏览器中正常运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:34