我如何在没有用户任何交互的情况下以html格式播放视频?

时间:2020-04-27 11:03:46

标签: html jquery video

我建立了吃豆人游戏,我想在游戏输掉的那一刻打开播放视频内容的模式,而无需点击按钮或进行任何其他操作。

我尝试过:

test.html:

<!DOCTYPE html>
<html>
<head>
    <script src="./jquery-3.4.1.slim.min.js"></script>
    <script src="./test.js"></script>
    <link rel="stylesheet" type="text/css" href="teststyle.css"> 
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<body>
    <button class="trigger">Click here to trigger the modal!</button>
    <div class="modal">
        <div class="modal-content">
            <div>
            <video id="video1" width="400" muted="muted" allow="autoplay">
                <source src="./assets/video/coffindance.mp4" allow="autoplay" type="video/mp4">
                Your browser does not support HTML5 video.
            </div>
            <button class="Restart">Restart</button>
         </div>
    </div>

</body>
</html>

test.js:

$(document).ready(function() {
  var modal = document.querySelector(".modal");

    modal.classList.toggle("show-modal");
    $('#video1')[0].muted=false;
    $('#video1')[0].play(); 


})

我收到此错误:

test.js:8 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. 

我该如何解决?

0 个答案:

没有答案