media HTML5媒体

基于 plyr HTML5播放器。

依赖

由于 plyr 脚本大小以及对视频播放并不是刚需的原因,因此采用一种延迟加载脚本的形式,可以通过全局配置配置来改变默认 CDN 路径(或使用本地路径),默认情况下使用 https://cdn.bootcdn.net/ajax/libs/plyr/3.5.10/plyr.min.jshttps://cdn.bootcdn.net/ajax/libs/plyr/3.5.10/plyr.css

  1. import { MediaModule } from '@delon/abc/media';

代码演示

media HTML5媒体 - 图1

MP4基础样例

最简单的用法。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'components-media-mp4',
  4. template: `
  5. <div class="mb-sm">
  6. <button nz-button (click)="media.player.play()">Play</button>
  7. <button nz-button (click)="media.player.pause()">Pause</button>
  8. <button nz-button (click)="media.player.restart()">Restart</button>
  9. </div>
  10. <media #media [source]="mp4" style="height: 200px"></media>
  11. `,
  12. })
  13. export class ComponentsMediaMp4Component {
  14. mp4 = `https://blz-videos.nosdn.127.net/1/OverWatch/AnimatedShots/Overwatch_AnimatedShot_Bastion_TheLastBastion.mp4`;
  15. }

media HTML5媒体 - 图2

自定义

指定 sourceoptions 来自定义播放器。

  1. import { Component } from '@angular/core';
  2. import { PlyrMediaSource, PlyrMediaType } from '@delon/abc/media';
  3. @Component({
  4. selector: 'components-media-custom',
  5. template: `
  6. <div class="mb-sm">
  7. <button nz-button (click)="play('video')">Change Play Video</button>
  8. <button nz-button (click)="play('audio')">Change Play Audio</button>
  9. </div>
  10. <media #media [source]="source" [options]="options" style="height: 200px;"></media>
  11. `,
  12. })
  13. export class ComponentsMediaCustomComponent {
  14. source: PlyrMediaSource = {
  15. type: 'video',
  16. sources: [
  17. {
  18. src: ``,
  19. },
  20. ],
  21. // 字幕
  22. tracks: [],
  23. };
  24. options = {
  25. // If you any problems, open `debug` and you can quickly find the issues
  26. debug: true,
  27. // controls: ['play-large'],
  28. i18n: {
  29. // For more parameters, please refer to: https://github.com/sampotts/plyr/blob/master/src/js/config/defaults.js#L151
  30. play: '播放',
  31. pause: '暂停',
  32. speed: '速度',
  33. normal: '正常',
  34. },
  35. };
  36. constructor() {
  37. this.play('video');
  38. }
  39. play(type: PlyrMediaType): void {
  40. this.source.type = type;
  41. if (type === 'video') {
  42. this.source.sources[0].src = `https://blz-videos.nosdn.127.net/1/OverWatch/AnimatedShots/Overwatch_AnimatedShot_Bastion_TheLastBastion.mp4`;
  43. } else {
  44. this.source.sources[0].src = `http://h5player.bytedance.com/video/music/audio.mp3`;
  45. }
  46. this.source = { ...this.source };
  47. }
  48. }

media HTML5媒体 - 图3

MP3基础样例

指定 type="audio" 来播放 MP3。

  1. import { Component } from '@angular/core';
  2. @Component({
  3. selector: 'components-media-mp3',
  4. template: `
  5. <div class="mb-sm">
  6. <button nz-button (click)="media.player.play()">Play</button>
  7. <button nz-button (click)="media.player.pause()">Pause</button>
  8. <button nz-button (click)="media.player.restart()">Restart</button>
  9. </div>
  10. <media #media type="audio" [source]="mp3" style="height: 200px"></media>
  11. `,
  12. })
  13. export class ComponentsMediaMp3Component {
  14. mp3 = `http://h5player.bytedance.com/video/music/audio.mp3`;
  15. }

API

media

成员说明类型默认值全局配置
[type]媒体类型audio, videovideo-
[source]媒体数据源string, PlyrMediaSource--
[options]播放器参数 plyr optionsany-
[delay]延迟初始化,单位:毫秒number--
(ready)准备就绪回调EventEmitter<Plyr>--