簡體   English   中英

如何更改暫停的html5視頻的當前時間並從該位置開始播放?

[英]How to change current time of a paused html5 video and start playing from that position?

我在這樣的頁面上有一個html 5視頻:

<video preload="none" id="movie" poster="http://my_poster_url.com/sourcefile.jpeg" controls>
     <source id="mp4" type="video/mp4" src="http://my_mp4.com/sourcefile.mp4"></source>
     <source id="webm" type="video/webm" src="http://my_webm.com/sourcefile.webm"></source>
</video>

在該視頻下方,我有一個導航,您可以在其中導航到不同的時間。 當視頻正在播放時,它完美運行,但是在暫停時出現以下錯誤:

InvalidStateError:嘗試使用一個不可用或不再可用的對象

這里的功能:

$('#my_video_navigation a').click(function () {
    var start_in_seconds=...//a variable that gets the time from a json based on the navigation index
    $("#movie").get(0).currentTime = start_in_seconds;
    $("#movie").get(0).play();
}

無論視頻是否暫停,我都添加了if / else語句:

if ($('#movie').get(0).paused) {
    $("#movie").get(0).play(); 
}

然后嘗試像上面一樣設置當前時間,甚至使用設置的超時功能設置它,以便它會等到視頻加載並播放后,但仍無法正常工作。 我做錯了什么或忘記了什么?

您應該更改currentTime媒體元素的領域,你可以看到這里的API文檔。 MDN上還有一個示例代碼

供參考,示例代碼在這里:

var mediaElement = document.getElementById('mediaElementID');
mediaElement.seekable.start();  // Returns the starting time (in seconds)
mediaElement.seekable.end();    // Returns the ending time (in seconds)
mediaElement.currentTime = 122; // Seek to 122 seconds
mediaElement.played.end();      // Returns the number of seconds the browser has played

其中mediaElementIDaudiovideo標簽的ID。

編輯:似乎我讀錯了你的問題。 我會進一步看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM