Media.duration

[與此頁面相關聯的功能, Windows 媒體播放器 SDK 是舊版功能。 它已被 MediaPlayer 取代MediaPlayer 已針對 Windows 10 和 Windows 11 優化。 Microsoft 強烈建議新程式代碼盡可能使用 MediaPlayer而不是 Windows 媒體播放器 SDK。 Microsoft 建議將使用舊版 API 的現有程式代碼重寫為盡可能使用新的 API。]

duration 屬性會擷取目前媒體項目的持續時間,以秒為單位。

語法

玩家currentMedia時間

可能的值

這個屬性是只讀數位(雙精度浮點數)。

備註

如果此屬性與 Player 中指定的媒體專案搭配使用。currentMedia,它可能不包含有效的值。

若要擷取不在用戶文檔庫中的檔案持續時間,您必須等候 Windows 媒體播放器 開啟檔案;也就是說,目前的OpenState必須等於 MediaOpen。 您可以藉由處理 Player 來確認這一點。OpenStateChange 事件或定期檢查 Player 的值openState

針對播放清單,可以在開啟個別媒體專案時擷取每個媒體項目的持續時間,而不是開啟播放清單時。

若要擷取這個屬性的值,需要連結庫的讀取許可權。 如需詳細資訊,請參閱 鏈接庫存取

下列 JScript 範例使用 媒體顯示目前媒體項目中剩餘時間的持續時間 。 名為 RemTime 的 HTML DIV 元素會顯示資訊。 HTML 定時器會每隔一秒更新 DIV 元素中的文字。

下列 JScript 程式代碼會啟動定時器:

// Execute the update() function at one-second intervals.
idTmr = window.setInterval("update()",1000);

下列 JScript 程式代碼會停止定時器:

window.clearInterval(idTmr);

使用 PlayerPlayStateChange 事件與 switch 語句,以判斷何時啟動和停止定時器。

每次定時器呼叫更新函式時,都會執行下列 JScript 程式代碼:

// Store the current position of the current media item.
var TimeNow = Player.controls.currentPosition;

// Display the time remaining information.
RemTime.innerHTML = "Seconds remaining: ";

// Subtract the current position from the duration of the current media.
// Use the Math.floor method to round the result down to the nearest integer.
RemTime.innerHTML += Math.floor(Player.currentMedia.duration - TimeNow);

需求

需求
版本
Windows 媒體播放器 7.0 版或更新版本。
DLL
Wmp.dll

另請參閱

媒體物件

Player.currentMedia

Player.PlayStateChange 事件

設定.mediaAccessRights

設定.requestMediaAccessRights