所以我有一个用于播放随机歌曲的脚本:http : //pastebin.com/G5JNFfY9
我不确定如何改变音量,有人能弄清楚吗(audio.volume 不起作用)?我还需要弄清楚如何在此脚本之外更改音量,因为音频是在函数中定义的。
另外,我需要一个有关如何更有效地缓冲它的示例。
你可以使用audio.volume
你只需要有一种方法来检索当前播放的音频
var collection=[];// final collection of sounds to play
var loadedIndex=0;// horrible way of forcing a load of audio sounds
var range = document.getElementById('range');
// remap audios to a buffered collection
function init(audios) {
for(var i=0;i<audios.length;i++) {
var audio = new Audio(audios[i]);
collection.push(audio);
audio.volume = range.value/100;
buffer(audio);
}
}
// did I mention it's a horrible way to buffer?
function buffer(audio) {
if(audio.readyState==4)return loaded();
setTimeout(function(){buffer(audio)},100);
}
// check if we're leady to dj this
function loaded() {
loadedIndex++;
if(collection.length==loadedIndex)playLooped();
}
// play and loop after finished
function playLooped() {
var audioIndex=Math.floor(Math.random() * (collection.length));
var audio=collection[audioIndex];
audio.play();
// you weren't saving off the index when it changes songs
loadedIndex = audioIndex;
setTimeout(playLooped,audio.duration*1000);
}
// change volume
function changeVolume(e) {
var value = Number(e.target.value)
collection[loadedIndex].volume = value/100;
e.target.nextElementSibling.childNodes[0].nodeValue = value + '%';
}
// the songs to be played!
init([
'http://popsplit.us/assets/music/1.mp3',
'http://popsplit.us/assets/music/2.mp3'
]);
<body>
<label for="range">Volume</label>
<input type="range" oninput="changeVolume(event)" id="range" min="0" max="100" />
<span class="output-volume">50%</span>
</body>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句