jQuery自动播放视频 [英] JQuery autoplay video

查看:1658
本文介绍了jQuery自动播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非常基本的HTML视频:

Hi I have some very basic html for a video:

<div class="video_holder">
      <video id="video_player" controls="controls" poster=""> 
        <source id="video_mp4" src="" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
      </video>
</div>

在我的js中:

 function playFile(e) {
      ...
      e.preventDefault()
      var selected = $(this);
      var path = selected.attr('href');
      $("#video_mp4").attr('src', path);
      // how do I get this thing to auto-play?????
  };

这非常适合将正确的路径注入 src 属性,并且我已验证路径正确且可访问。但是,我无法使视频在加载时自动播放。

This works great for injecting the correct path into the src attribute, and I have verified the path is correct and accessible. However, I cannot get the video to autoplay on load.

我尝试过:

document.getElementById("video_player").play();

$('#video_player').trigger('play');

$('#video_player').play();

如何触发视频自动播放?

How can I trigger the video to autoplay?

非常感谢您的帮助。

推荐答案

您可以使用:

$("#video_player")[0].play();

或:

$("#video_player")[0].autoplay = true;

或:

document.getElementById("video_player").setAttribute('autoplay', true);

任何适合您的。使用 $('#video_player')。play(); 指的是jQuery中不存在的方法 play 并且应该引用 $(#video_player)找到的对象。

Whatever suits you. Using $('#video_player').play(); you are referencing to non-existing method play in jQuery and you should reference to the object found by $("#video_player").

要更改的src JS中的视频,您只需要这样的东西:

To change the src of the video in JS, you just simply need something like that:

function playFile() {
    var video = $("#video_player");
    video[0].src = "http://www.yoursrc.com/video_new.mp4";
    video[0].load();
    video[0].play();
};



注意:

在Chrome中,如果要使用 autoplay 属性,还需要添加 muted 属性。请参阅: https://developers.google.com/web / updates / 2017/09 / autoplay-policy-changes


NOTE:
In Chrome, you should also need to add muted property if you want to use the autoplay attribute. Refer to : https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

这篇关于jQuery自动播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆