javascript - 如何包装 HTML5 视频元素的 currentTime 属性?

标签 javascript html video html5-video

我正在开发一个视频播放器作为学习模块系统的一部分。某些模块要求用户无法(轻松)在视频中向前跳转。我知道这通常会被认为是一种糟糕的用户体验,但在这种情况下这是必不可少的。

我已经能够通过执行以下操作来覆盖 video.currentTime 属性。它可以防止跳过,但我需要能够有条件地打开和关闭它。

Object.defineProperty(videoElement, 'currentTime', {
  enumerable: true,
  configurable: true,
  get: function() {
    // return from original currentTime
  },
  set: function(newValue) {
    // intercept values here
  }
});

如何在 getter 和 setter 中引用原始 video.currentTime 属性?不幸的是,Object.getOwnPropertyDescriptor(videoElement, 'currentTime'); 返回undefined

最佳答案

可能有一种更优雅的方法来做到这一点,但是在对象上调用原型(prototype)(在本例中为 HTMLMediaElement 原型(prototype))的 currentTime getter 将为您提供实际的当前时间。因此这将阻止设置但允许获取:

  Object.defineProperty(videoElement, 'currentTime', {
      enumerable: true,
      configurable: true,
      get: function() {
        var ct = Object.getOwnPropertyDescriptor(HTMLMediaElement.prototype,
           'currentTime').get.call(this);
        return ct;
      },
      set: function(newValue) {
        // intercept values here
      }
    });

关于javascript - 如何包装 HTML5 视频元素的 currentTime 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326267/

相关文章:

android - 使用 Air android 视频播放重新打开设备屏幕后出现黑屏

video - 对于简单的视频格式,我有哪些选择?

javascript - 带有来自本地主机 : Origin does not match any app domain 的 JS 的 Dropbox API 选择器

html - 文本(特殊 unicode,如 ⇑)在 IE11 中不垂直居中

html - 使用XPath进行灵活的文本检索

ipad - 如何隐藏/禁用 iPad 标准 HTML5 视频控件中的全屏按钮?

Javascript - 使用特定公式模 11 创建数字

javascript - 计算文本区域中的特定字符

javascript - 在我的案例中如何检测点击和操作元素

php - 命名 php 文件