Media.duration

[与本页关联的功能 (Windows 媒体播放器 SDK) 是一项旧版功能。 它已被 MediaPlayer 所取代。 MediaPlayer 已针对 Windows 10 和 Windows 11 进行了优化。 Microsoft 强烈建议新代码应尽可能地使用“MediaPlayer”,而不是“Windows 媒体播放器 SDK”。 如果可能,Microsoft 建议应重写使用旧 API 的现有代码,以尽可能地使用新的 API。]

“duration”属性可检索当前媒体项的持续时间(以秒为单位)

语法

“player.currentMedia.duration”

可能值

此属性是只读的“数字”(“双”)。

备注

如果此属性与除“Player.currentMedia”中指定的媒体项之外的媒体项一起使用,则可能不会包含有效值。

要检索不在用户库中的文件的持续时间,必须等待 Windows Media Player 打开该文件;也就是说,当前 OpenState 必须等于 MediaOpen。 可以对此进行验证,方法是处理“Player.OpenStateChange”事件,或定期检查“Player.openState”的值

对于播放列表,可以在打开单个媒体项时而不是打开播放列表时检索每个媒体项的持续时间。

要检索此属性的值,需要对库具有读取访问权限。 有关详细信息,请参阅库访问权限

以下 JScript 示例使用“Media.duration”显示当前媒体项的剩余时间。 名为 RemTime 的 HTML DIV 元素显示了该信息。 HTML 计时器会每秒更新一次 DIV 元素中的文本。

以下 JScript 代码可启动计时器:

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

以下 JScript 代码可停止计时器:

window.clearInterval(idTmr);

使用“Player.PlayStateChange”事件和“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 Media Player 版本 7.0 或更高版本。
DLL
Wmp.dll

另请参阅

媒体对象

Player.currentMedia

Player.PlayStateChange 事件

Settings.mediaAccessRights

Settings.requestMediaAccessRights