Tutorial: player-workflows | Video.js Documentation

播放器工作流

本文档概述了在高级播放器工作流中使用 Video.js 的许多注意事项。请务必先阅读 《setup 指南》

访问已在页面上创建的播放器

创建实例后,可以通过两种方式全局访问该实例:

  1. 通过调用 videojs('example_video_id'); 访问
  2. 直接通过 videojs.players.example_video_id; 访问

删除播放器

无论使用什么术语,web 应用程序都变得越来越普遍。web 页面并不是所有的东西都是静态的、只需加载一次就结束的!这意味着开发人员需要能够管理视频播放器的整个生命周期——从创建到销毁。Video.js 支持通过dispose() 方法删除播放器。

dispose()

此方法适用于所有 Video.js 播放器和组件。这是从 DOM 和内存中删除 Video.js 播放器的唯一受支持的方法。例如,以下代码设置播放机,然后在媒体播放完成后对其进行删除:

  1. var player = videojs('my-player');
  2. player.on('ended', function() {
  3. this.dispose();
  4. });

Calling dispose() will have a few effects:

调用 dispose() 会产生以下效果:

  1. 在播放器上触发一个 "dispose" 事件,允许运行您整合的所有自定义清理任务。
  2. 从播放器中删除所有事件侦听器。
  3. 删除播放器的 DOM 元素。

此外,这些动作递归地应用于播放器的所有子组件。

注意:不要通过标准的 DOM 删除方法删除播放器:这将使侦听器和内存中的其他对象无法清理!

检查播放器是否被删除

有时,了解代码中的播放器引用是否过时很有用。isDisposed() 方法在所有组件(包括播放器)上都可用于确认这件事。

处理未删除的播放器

当看到这个错误:

  1. TypeError: this.el_.vjs_getProperty is not a function

  1. TypeError: Cannot read property 'vdata1234567890' of null

这可能是因为在不使用 `dispose() 的情况下从 DOM 中删除了播放器或组件。它通常意味着试图在播放器上触发事件或调用方法。

显示和隐藏播放器

不建议您尝试切换 Video.js 播放器的 visibilitydisplay。相反,播放器应该根据需要创建和删除

特别是像在弹窗(modal)/遮罩(overlay)中显示播放器这样的用例,建议您在弹窗打开时创建播放器,并在弹窗关闭时进行删除,而不是在 DOM 元素中保留一个隐藏的 Video.js 播放器。

这在涉及内存/资源使用的情况下(例如移动设备)尤其重要。

根据使用的库/框架,实现可能如下所示:

  1. modal.on('show', function() {
  2. var videoEl = modal.findEl('video');
  3. modal.player = videojs(videoEl);
  4. });
  5. modal.on('hide', function() {
  6. modal.player.dispose();
  7. });

改变播放器的音量

可通过播放机上的 volume 方法更改播放机的音量。volume 方法接受 0-1 之间的数字。不带参数调用它将返回当前音量。

示例

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get
  5. var howLoudIsIt = myPlayer.volume();
  6. // set
  7. myPlayer.volume(0.5); // Set volume to half
  8. });

也可以使用 muted 方法设置静音(而不实际更改音量的值)。不带参数调用它将返回播放器上静音的当前状态。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get, should be false
  5. console.log(myPlayer.muted());
  6. // set to true
  7. myPlayer.muted(true);
  8. // get should be true
  9. console.log(myPlayer.muted());
  10. });

全屏播放

要检查播放器当前是否全屏,可以像这样调用播放器上的 isFullscreen 方法。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // get, should be false
  5. console.log(myPlayer.isFullscreen());
  6. // set, tell the player it's in fullscreen
  7. myPlayer.isFullscreen(true);
  8. // get, should be true
  9. console.log(myPlayer.isFullscreen());
  10. });

调用 requestFullscreen 指示播放器进入全屏模式。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.requestFullscreen();
  5. });

调用 exitFullscreen 退出全屏

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.requestFullscreen();
  5. myPlayer.exitFullscreen();
  6. });

播放相关的方法

play 可以用于播放指定了源的播放器。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.play();
  5. });

pause 可用于暂停正在播放的播放器。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.play();
  5. myPlayer.pause();
  6. });

paused 可用于确认当前播放器是否暂停。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // true
  5. console.log(myPlayer.paused());
  6. // false
  7. console.log(!myPlayer.paused());
  8. myPlayer.play();
  9. // false
  10. console.log(myPlayer.paused());
  11. // true
  12. console.log(!myPlayer.paused());
  13. myPlayer.pause();
  14. // true
  15. console.log(myPlayer.paused());
  16. // false
  17. console.log(!myPlayer.paused());
  18. });

currentTime 可以设置或获取当前的播放位置/时间 (以秒为单位)。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // set current time to 2 minutes into the video
  5. myPlayer.currentTime(120);
  6. // get the current time, should be 120 seconds
  7. var whereYouAt = myPlayer.currentTime();
  8. });

duration 可以获取当前正在播放的视频的总长度(时间)

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. var lengthOfVideo = myPlayer.duration();
  5. });

remainingTime 可以获取当前视频剩余秒数。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. myPlayer.currentTime(10);
  5. // should be 10 seconds less than duration
  6. console.log(myPlayer.remainingTime());
  7. });

buffered 提供一个 timeRange 对象,该对象表示已经缓冲的,准备在将来某个时间播放的时间范围。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. var bufferedTimeRange = myPlayer.buffered();
  5. // number of different ranges of time have been buffered.
  6. // Usually 1
  7. var numberOfRanges = bufferedTimeRange.length,
  8. // Time in seconds when the first range starts.
  9. // Usually 0
  10. var firstRangeStart = bufferedTimeRange.start(0),
  11. // Time in seconds when the first range ends
  12. var firstRangeEnd = bufferedTimeRange.end(0),
  13. // Length in seconds of the first time range
  14. var firstRangeLength = firstRangeEnd - firstRangeStart;
  15. });

bufferedPercent 可以获取当前视频缓冲的百分比。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // example 0.11 aka 11%
  5. var howMuchIsDownloaded = myPlayer.bufferedPercent();
  6. });

处理播放器上的 source(源文件)和 poster(预览图片/海报)

通过 API 将 source 传递给播放器。(这也可以使用 options 完成)

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src('http://www.example.com/path/to/video.mp4');

当提供一个字符串作为 source 时,Video.js 将尝试从文件扩展名推断视频类型,但这种推断并非在所有情况下都有效。建议 source 通过包含 type 的对象格式提供,如下所示。

Source 对象(或元素):包含 source 源文件信息的 javascript 对象。如果希望播放器使用 type 确定是否支持该文件,请使用此方法。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});

Sources 对象数组: 要提供 source 源文件的多个版本,以便使用 HTML5 跨浏览器播放,可以使用对象数组格式。Video.js 将监测支持哪个版本并加载该文件。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src([
  3. {type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'},
  4. {type: 'video/webm', src: 'http://www.example.com/path/to/video.webm'},
  5. {type: 'video/ogg', src: 'http://www.example.com/path/to/video.ogv'}
  6. ]);

通过 API 更改或设置 poster。(这也可以通过 options 完成)

  1. var myPlayer = videojs('example_video_1');
  2. // set
  3. myPlayer.poster('http://example.com/myImage.jpg');
  4. // get
  5. console.log(myPlayer.poster());
  6. // 'http://example.com/myImage.jpg'

访问播放器的 Tech

可以通过 tech() 访问播放器上的 tech。传递任何参数都将屏蔽它的警告提示。

  1. var myPlayer = videojs('some-player-id');
  2. myPlayer.src({type: 'video/mp4', src: 'http://www.example.com/path/to/video.mp4'});
  3. myPlayer.ready(function() {
  4. // tech() will log warning without any argument
  5. var tech = myPlayer.tech(false);
  6. });

使用 Video.js 与…

jQuery

React

See ReactJS integration example

Ember

Angular

Vue

See Vue integration example