[英]JS and JQuery Audio Volume Does Not Initialize
The Audio player loads and initializes, but the controls disappear once I pass the third song (see >= 3
test). 音频播放器加载并初始化,但是一旦我通过第三首歌曲,控件就会消失(请参阅
>= 3
测试)。 I'm not certain if that's an incredible coincidence but it seems likely I have broken something. 我不确定这是否是一个不可思议的巧合,但似乎我已经打破了某些东西。 Why do the audio controls vanish?
为什么音频控件消失了?
Also, the volume controls do not initialize. 此外,音量控件不会初始化。 Does anyone know why?
有人知道为什么吗?
<script>
window.onload = function() {
var number = Math.floor((Math.random() * 10) + 1);
if(number >= 3) {
document.getElementById("audio").innerHTML = "<audio id='vid' src='remix.mp3' type='audio/mpeg' autoplay='true' loop='true'></audio>";
} else {
document.getElementById("audio").innerHTML = "<audio id='vid' src='lose.mp3' type='audio/mpeg' autoplay='true' loop='true'></audio>";
}
};
(function(){
var vid = document.getElementById("vid");
vid.volume = 0.2;
});
</script>
<script>
jQuery(function($) {
$("#vid").prop('volume', 0.2);
window.setVolume = function(bgAudio,vol) {
sounds[bgAudio].volume = 0.33;
}
});
</script>
<div id="audio">
</div>
There is no need to replace the whole audio
tag. 无需替换整个
audio
标签。 Just change the src
attribute: 只需更改
src
属性即可:
<audio id='vid' src='remix.mp3' type='audio/mpeg' autoplay='true' loop='true'></audio>
... ...
$('#vid').attr('src', 'remix.mp3');
EDIT 编辑
Your code is a mess, I've tried to refactor it a bit: 您的代码一团糟,我尝试对其进行一些重构:
(function() { var number = Math.floor((Math.random() * 10) + 1); var player = $('#vid'); player.attr('src', number >= 3 ? 'remix.mp3' : 'lose.mp3'); player.prop('volume', '0.2'); player[0].play(); //run the audio track //not sure about this function window.setVolume = function(bgAudio, vol) { sounds[bgAudio].volume = vol; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> <audio id='vid' src='' type='audio/mpeg' loop='true'></audio>
And check mp3 file names and location. 并检查mp3文件名和位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.