简体   繁体   English

使用 jQuery 实现音频淡入淡出

[英]Audio Fade in and Fade Out with jQuery

I'm trying to fade in and fade out but I don't know what's wrong in my code.我正在尝试淡入淡出,但我不知道我的代码有什么问题。 Fade in works but fade out does not.淡入有效但淡出没有。

Below the code:代码下方:

        var mPlayer = document.getElementById("pad-c");
        $(mPlayer).prop("volume", 0.0);

        var isPlaying = false;

        function playAudio() { 
            mPlayer.play(); 
            isPlaying = true;
        } 

        function pauseAudio() { 
            mPlayer.pause();
            isPlaying = false;
        } 
        function playPauseC() {
          if (isPlaying == true) {
            $(mPlayer).animate({volume: 0.1}, 5000);
            pauseAudio();
          } else {
            $(mPlayer).animate({volume: 1.0}, 5000);
            playAudio();
          }
        }

Right now you are pausing the audio at the same time as the fadeout.现在您在淡出的同时暂停音频。 Use the complete parameter on .animate() to pause the audio after the fadeout is complete.使用.animate()上的complete参数在.animate() complete暂停音频。

function playPauseC(){
  if(isPlaying == true) {
    $(mPlayer).animate({volume: 0.1}, 5000, function() {
      pauseAudio();
    });
  } else {
    $(mPlayer).animate({volume: 1.0}, 5000);
    playAudio();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM