2017-10-11 99 views
0

單擊暫停按鈕時如何使聲音淡出?單擊暫停按鈕時的fadeOut()聲音

<script> 
var sounds = [ 
    "sounds/royksopp.mp3", 
    "sounds/9thwonder.mp3", 
    "sounds/thisbeat.mp3", 
    "sounds/mosdef.mp3", 
    "sounds/bewater.mp3", 
    "sounds/boutdre.mp3", 
    "sounds/masterflash.mp3", 
    "sounds/2ep.mp3", 
    "sounds/drewestcoast.mp3", 
    "sounds/poetry.mp3", 
    "sounds/mfdoom.mp3", 
    "sounds/oizo.mp3", 
]; 

function StartOrStop(audioFile) { 
    srcAudio = sounds[Math.floor(Math.random() * sounds.length)]; 
    var audie = document.getElementById("myAudio"); 
    audie.addEventListener('ended', function() { 
     this.currentTime = 0; 
     this.play(); 
    }, false); 

    if (audie.paused == false) { 
     audie.pause(); 
    } else { 
     audie.src = srcAudio; 
     audie.play(); 
    } 
} 
</script> 

然後,我把它叫做:

<audio id="myAudio"></audio> 

<button id="idj-play-button" onclick="StartOrStop()" class="btn btn-lg"><i class="glyphicon glyphicon-play"></i> 
</button> 

<button id="idj-pause-button" onclick="StartOrStop()" class="btn btn-lg hide"><i class="glyphicon glyphicon-refresh"></i> 
</button> 

我需要改變我的if/else語句audie.pause()什麼?我試過audie.animate({volume: 0}, 1000);,但沒有奏效。

謝謝。

+0

使用'.animate'您需要的音頻是一個jQuery元素:'$(audie).animate(...)' – davidchoo12

+0

如果我使用'$(audie).animate({volume:0},500);'而不是'audie.pause();'它成功將音量降低到0,但是當我再次單擊播放按鈕時,沒有任何反應。 – Rui

回答

0

試試這個:

<script> 
    var sounds = [ 
     "sounds/royksopp.mp3", 
     "sounds/9thwonder.mp3", 
     "sounds/thisbeat.mp3", 
     "sounds/mosdef.mp3", 
      "sounds/bewater.mp3", 
      "sounds/boutdre.mp3", 
      "sounds/masterflash.mp3", 
      "sounds/2ep.mp3", 
     "sounds/drewestcoast.mp3", 
      "sounds/poetry.mp3", 
      "sounds/mfdoom.mp3", 
     "sounds/dreams.mp3", 
     "sounds/oizo.mp3", ]; 

    function Start(audioFile) { 
     srcAudio = sounds[Math.floor(Math.random()*sounds.length)]; 
     var audie = document.getElementById("myAudio"); 
     audie.addEventListener('ended', function() { 
     this.currentTime = 0; 
     this.play(); 
     }, false); 

       $(audie).animate({volume: 0.9}, 0); 
       audie.src = srcAudio; 
       audie.play(); 

      } 

    function Stop(audioFile) { 
     var audie = document.getElementById("myAudio"); 

       $(audie).animate({volume: 0}, 400); 

      } 
</script> 

HTML:

<audio id="myAudio"></audio> 

<button id="idj-play-button" onclick="Start()" class="btn btn-lg"><i class="glyphicon glyphicon-play"></i> 
</button> 

<button id="idj-pause-button" onclick="Stop()" class="btn btn-lg hide"><i class="glyphicon glyphicon-refresh"></i> 
</button> 
0

audie.animate({volume: 0}, 1000);只有當你有jQuery時才能工作。

你可以做這樣的修改:

if (audie.paused == false) { 
    let interval = setInterval(() => { 
     if (audie.volume == 0) { 
      audie.pause(); 
      clearInterval(interval); 
     } 
     audie.volume -= 0.05; 
    }, 800); 
} 
+0

嗨,當我點擊暫停按鈕時,它會降低音量,但仍然可以聽到,非常低。當我再次點擊播放按鈕時,沒有任何反應。 – Rui

相關問題