2016-10-03 2 views
0

HTML5 오디오 객체를 음소거하는 음소거 버튼이 있습니다. 그것은 트랙을 음소거하지만 페이드 인/아웃 효과를 추가하고 싶습니다. audio.animate({volume: 0}, 2000);을 추가했지만 작동하지 않습니다.HTML5 음소거 버튼에 페이드 효과를 적용하는 방법

아이디어가 있으십니까?

감사합니다.

audio = new Audio(); 
 
audio.src = "http://myst729.qiniudn.com/within-temptation_pale.mp3" 
 
audio.play(); 
 

 
$(".mute").on("click tap", function(){ 
 
\t if (audio.muted) { 
 
      audio.animate({volume: 1}, 2000); 
 
      audio.muted = false; 
 
      $(this).text("mute"); 
 

 
     } else { 
 
      audio.muted = true; 
 
      audio.animate({volume: 0}, 2000); 
 
      $(this).text("unmute"); 
 
     } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button class="mute">mute</button>

+0

이'audio.animate 같은 애니메이션 기능에 –

+0

@KarthikeyanSekar을 모든 것을 넣어 (: 2000 {1 권 = 거짓 audio.muted}) ;'? – DannyBoy

+1

[HTML5

답변

3

audio 객체 JQuery와 .animate() 함수를 사용하기 위해 JQuery와 오브젝트이어야한다. audio.animate$(audio).animate으로 변경하면됩니다.

또한 audio.muted = ... 문은 음악을 즉시 끄고 켜기 때문에 애니메이션이 들리지 않습니다.

동작하는 예제 :

audio = new Audio(); 
 
audio.src = "http://myst729.qiniudn.com/within-temptation_pale.mp3" 
 
audio.play(); 
 

 
$(".mute").on("click tap", function() { 
 
    var $btn = $(this); 
 
    
 
    var muted = audio.muted; 
 
    if (muted) audio.muted = false; // It cannot be animated if it's muted 
 
    
 
    $btn.prop('disabled', true); // Optional 
 
    $(audio).animate({volume: muted ? 1 : 0}, 2000, function() { 
 
    audio.muted = !muted; 
 
    $btn.text(muted ? "mute" : "unmute"); 
 
    $btn.prop('disabled', false); // Optional 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button class="mute">mute</button>

+0

분명히 오디오는 재생할 수 없습니다. 'audio.muted = true' 인 경우 애니메이션이 적용되므로 음소거 애니메이션이 제대로 작동하지 않습니다. 이 문제를 고치면 내 대답이 향상되었습니다.) –

관련 문제