2017-03-01 70 views
0

無法弄清楚如何重新啓動和恢復視頻共:如何取消靜音並重新啓動視頻?

var player = document.getElementById('player'); 
var video = document.getElementById('video'); 
var play = document.getElementById('play'); 

video.addEventListener('canplay', function() { 
    video.muted = true; 
    video.play(); 
}); 

play.addEventListener('click', function() { 
    player.classList.add('is-open'); 
    video.muted = false; 
    video.load(); 
    video.play(); 
}); 
+0

if(video.muted)video.muted = false;如果(!video.muted)video.muted = true; –

+0

@OusmaneDiaw不工作 – walterdesign

+0

你見過這個http://stackoverflow.com/questions/33060291/video-mute-unmute-button-javascript –

回答

0

會發生什麼事是,當你處理click事件,將重新觸發的canplay事件,因爲涉及以後它會重新靜音狀態。

你可以使用一些標誌或退訂canplay事件中,你需要播放視頻,第二次 - 或者設置在視頻元素本身autoplaymuted不使用canplay事件。

var first = true; 

video.addEventListener('canplay', function() { 
    if (first) { 
     video.muted = true; 
     video.play(); 
     first = false; 
    } 
}); 
相關問題