Html 有没有办法设置默认的 HTML5 视频音量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7582385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:50:42  来源:igfitidea点击:

Is there a way to set the default HTML5-Video volume?

htmlhtml5-video

提问by supercoolville

HTML5 videos always start at 100% volume.

HTML5 视频始终以 100% 的音量开始。

How can I make them start at 50% volume?

我怎样才能让它们从 50% 的音量开始?

回答by marksyzm

You can affect the volumeproperty of the <video>element as follows:

您可以按如下方式影响元素的volume属性<video>

document.getElementsByTagName('video')[0].volume = 0.5;

If using jQuery then you can use their propmethod to alter the volume in a jQuery collection object like so:

如果使用 jQuery,那么您可以使用他们的prop方法来更改 jQuery 集合对象中的音量,如下所示:

$("video").prop("volume", 0.5);

This will alter all DOM elements in the collection.

这将改变集合中的所有 DOM 元素。

回答by dwelle

Assuming you're good with mixing JS into your HTML, you can leverage one of the events, such as loadstart:

假设您擅长将 JS 混合到 HTML 中,您可以利用其中一个事件,例如loadstart

<video onloadstart="this.volume=0.5" ...>

caveat:browser support - works in latest Chrome, and FF (there's a visual bug, though). Haven't tested others.

警告:浏览器支持 - 适用于最新的 Chrome 和 FF(不过有一个视觉错误)。没有测试过其他人。

回答by Ernestas Stankevi?ius

var video = document.getElementById('player');
video.volume = 0.5;

P.S. Use thisscript for compatibility.

PS 使用脚本是为了兼容性。

回答by Michael Hall

<div>
    <video id="sampleMovie" src="mp4/Premier delivery.mp4" width="777" height="582.75" controls autoplay ></video>
        <script>
        var video = document.currentScript.parentElement;
        video.volume = 0.1;
        </script>
    </div>

Works perfectly!

完美运行!

回答by Paul

If you don't want to mess with javascript, you can do it like this:

如果你不想弄乱 javascript,你可以这样做:

<video muted="">
    <source src="yourvideo.mp4" type="video/mp4">
</video>

回答by Stan

With jQuery need to use a little trick:

用jQuery需要用到一个小技巧:

$('#your_video_id').get(0).volume = 0;

回答by tmpsa

Setting the defaultvolume, using jquery:

使用 jquery设置默认音量:

$(function() {
    $("video").each(function(){ this.volume = 0.5; });
});