在加载页面时,我似乎无法调整这个音频元素的音量。以下是代码
var bleep = new Audio();
bleep.src = "Projectwebcrow2.mp3";
bleep.volume = 0.1;
发布于 2019-09-22 06:21:07
如果您使用的是音频标签,只需在Javascript中获取DOM节点并操作音量属性即可。
var audio = document.querySelector('audio');
// Getting
console.log(volume); // 1
// Setting
audio.volume = 0.5; // Reduce the Volume by Half
您设置的数字应在0.0到1.0的范围内,其中0.0是最安静的,1.0是最大的。注意:如果您设置的值不在0.0到1.0的范围内,则JS将抛出IndexSizeError。
对于WEB音频API,先编写一些代码,我们将加载音乐文件并使用Web AUDIO API播放它。
var ctx = new webkitAudioContext();
function loadMusic(url) {
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.responseType = 'arraybuffer';
req.onload = function() {
ctx.decodeAudioData(req.response, playSound);
};
req.send();
}
function playSound(buffer) {
var src = ctx.createBufferSource();
src.buffer = buffer;
src.connect(ctx.destination);
// Play now!
src.noteOn(0);
}
发布于 2019-09-22 06:32:44
应该能行得通。你需要给我们更多的细节(我不能评论)。如果还没有,只需添加
bleep.play()
另外,在大多数浏览器中,默认情况下自动播放音频是禁用的,也许这就是原因。
发布于 2019-09-22 07:00:03
如果您的音乐是页面中的一个元素,您可以使用:
var music = document.getElementById("myMusic");
music.volume = 0.2;
如果不是,请使用:
var music = new Audio('audio/correct.mp3');
music.volume = 0.2;
你可以查看https://www.w3schools.com/tags/av_prop_volume.asp
了解更多详细信息
https://stackoverflow.com/questions/58044503
复制相似问题