How to disable auto-play for local video in iframe

2020-02-26 04:28发布

How to disable auto-play for video when src is from my local pc?

<iframe width="465" height="315" src="videos/example.mp4"></iframe>

I have tried the following, but it doesn't work:

  • src="videos/example.mp4?autoplay=0"
  • src="videos/example.mp4?autoplay=false"
  • src="videos/example.mp4?autostart=0"
  • src="videos/example.mp4?autostart=false"

7条回答
别忘想泡老子
2楼-- · 2020-02-26 04:45

If you are using HTML5, using the Video tag is suitable for this purpose.

You can use the Video Tag this way for no autoplay:

<video width="320" height="240" controls>
    <source src="videos/example.mp4" type="video/mp4">
</video>

To enable auto-play,

<video width="320" height="240" controls autoplay>
    <source src="videos/example.mp4" type="video/mp4">
</video>
查看更多
家丑人穷心不美
3楼-- · 2020-02-26 04:47

Replace the iframe for this:

<video class="video-fluid z-depth-1" loop controls muted>
  <source src="videos/example.mp4" type="video/mp4" />
</video>
查看更多
Evening l夕情丶
4楼-- · 2020-02-26 04:51

What do you think about video tag ? If you don't have to use iframe tag you can use video tag instead.

<video width="500" height="345" src="hey.mp4"  />

You should not use autoplay attribute in your video tag to disable autoplay.

查看更多
Lonely孤独者°
5楼-- · 2020-02-26 04:52

You can set the source of the player as blank string on loading the page, in this way you don't have to switch to video tag.

var player = document.getElementById("video-player");
player.src = "";

When you want to play a video, just change its src attribute, for example:

function play(source){
   player.src = source;
}
查看更多
淡お忘
6楼-- · 2020-02-26 04:56

I've tried all the possible solutions but nothing worked for local video bindings. I believe best solution would be to fix using jQuery if you still wants to use iframes.

$(document).ready(function () {
    var ownVideos = $("iframe");
    $.each(ownVideos, function (i, video) {                
        var frameContent = $(video).contents().find('body').html();
        if (frameContent) {
            $(video).contents().find('body').html(frameContent.replace("autoplay", ""));
        }
    });
});

Note: It'll find all the iframes on document ready and loop through each iframe contents and replace/remove autoplay attribute. This solution can be use anywhere in your project. If you would like to do for specific element then use the code under $.each function and replace $(video) with your iframe element id like $("#myIFrameId").

查看更多
对你真心纯属浪费
7楼-- · 2020-02-26 05:00
<iframe width="420" height="315"
    src="https://www.youtube.com/embed/aNOgO7aNslo?rel=0">
</iframe>

You're supposed to put the characters ?rel=0 after the YouTube video unique link and before the quotations. it worked for me

查看更多
登录 后发表回答