简体   繁体   English

Javascript添加事件侦听器以更改音量

[英]Javascript Add Event Listener for change in Audio Volume

I am trying to save the volume of a playing music file in a cookie so when the page is reloaded, the volume the user chose last is maintained, rather than turning up super loud or whatever. 我正在尝试将一个播放音乐文件的音量保存在cookie中,这样当重新加载页面时,用户最后选择的音量就会被维持,而不是超级响亮或者其他任何东西。

Here's my test code for the eventlistener: 这是eventlistener的测试代码:

var myAudio = document.getElementById("audio1");

myAudio.addEventListener('change',alert("Audio Volume Changed"),true};

However, it does not respond when I change the volume. 但是,当我更改音量时它没有响应。 I've searched and despite it being something I think is pretty practical, there's no information on it. 我搜索过,尽管它是我认为非常实用的东西,但没有相关信息。

You're looking for the "volumechange" event. 您正在寻找“volumechange”事件。

var audio = document.getElementById('sample');

audio.addEventListener('volumechange', function() {
    console.log('changed.', arguments);
}, false);

I'm using the bubble phase(as opposed to capture) in this example. 我在这个例子中使用了泡沫阶段(而不是捕获)。

http://jsfiddle.net/426E6/ http://jsfiddle.net/426E6/

http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#event-definitions http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#event-definitions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM