2017-03-01 46 views
-1

所以我已经用于播放随机歌曲的脚本: http://pastebin.com/G5JNFfY9如何更改此脚本中的音量? (JavaScript的)

我不完全知道我怎么可能会改变音量,任何人都可以算出它(audio.volume不工作)?由于音频是在函数中定义的,因此我还需要弄清楚如何更改此脚本以外的音量。

此外,我需要一个关于如何更有效地缓冲这个问题的例子。

+0

您可以使用“格式代码”按钮的方式('{}')把你的问题中的代码。另外,请为每个问题选择一个问题。 =) – Ryan

+0

好的,我会照着指示去做。 –

回答

0

您可以使用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>

+0

谢谢,当我有机会给你我的结果时,我会试试这个。 –

+0

上面的代码片段在浏览器中运行,它可以让你改变音量 – Mobius

+0

好吧,我看到了变化。 –