2013-10-21 179 views
0

我正在努力與自動暫停,然後再次播放(手動)問題。視頻暫停後2秒這是好的。現在有另一個按鈕,點擊該按鈕後,視頻應該從暫停的位置開始播放。暫停後html5視頻無法播放

這裏是我的代碼(HTML):

<div class="restart">Restart after pause</div> 
<video id="video" width="400" height="400" id="video" src="http://media.w3.org/2010/05/sintel/trailer.mp4" controls autoplay /> 

的jQuery:

jQuery(document).ready(function ($) { 

    $('#video').get(0).play(); 


    setInterval(function() { 
     if ($('#video').get(0).currentTime >= 2) { 
      $('#video').get(0).pause(); 
     } 
    }, 100); 


    $(".restart").click(function(event){ 

     $('#video').get(0).play(); 


    setInterval(function() { 
     if ($('#video').get(0).currentTime >= 6) { 
      $('#video').get(0).pause(); 
     } 
    }, 100); 
    }); 

}); 

我絕對不是在正確的道路上就當前的時間和設置time.Please建議。

的的jsfiddle是提前

http://jsfiddle.net/squidraj/KmQxL/10/

感謝。因爲它們是不必要的與現有的timeupdate事件http://jsfiddle.net/VZuuF/1/

我擺脫你的setIntervals的:

+1

如果你不清除的時間間隔,將始終保持自currentTime的暫停將是大於或等於2 – Ronnie

+1

也,而不是間隔,請使用'timeupdate'事件 – Ronnie

+0

好去處....它的工作現在..非常感謝你。小提琴現在更新了。 –

回答

2

請仔細閱讀本小提琴。

$(document).ready(function() 
{ 
    var checkedFirst = false; 
    var checkedSecond = false; 
    $player = $('#video').get(0); 
    $player.play(); 

    $player.addEventListener('timeupdate', function(event) 
    { 
     console.log($player.currentTime); 
     if ($player.currentTime >= 2 && !checkedFirst) 
     { 
      checkedFirst = true; 
      $player.pause(); 
     } 

     if ($player.currentTime >= 6 && !checkedSecond) 
     { 
      checkedSecond = true; 
      $player.pause(); 
     } 
    }); 


    $(".restart").click(function(event) 
    { 
     $player.play(); 
    }); 
}); 
+0

輝煌。我修改了一下,所以現在我有兩個控件。非常感謝您的幫助。我今天早上開始了,現在是晚上8點。學了很多東西,再次感謝。 –

+0

嘿羅尼再次感謝。我只是在我的原始代碼中執行並完美運行。非常感謝。 –

+0

真棒很高興它爲你工作。不要忘記接受答案 – Ronnie

相關問題