video视频播放器

介绍

原生video实现的视频播放器

安装

  1. import { createApp } from 'vue';
  2. import { Video } from '@nutui/nutui';
  3. const app = createApp();
  4. app.use(Video);

代码演示

基础用法

  1. <nut-video
  2. :source="source"
  3. @play="play"
  4. @pause="pause"
  5. @playend="playend">
  6. </nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. }
  7. });
  8. const play = (elm: any) => console.log('play', elm);
  9. const pause = (elm: any) => console.log('pause', elm);
  10. const playend = (elm: any) => console.log('playend', elm);
  11. return { play, pause, playend, ...toRefs(state) };
  12. }

自动播放

autoplay 属性设置视频自动播放

  1. <nut-video :source="source" :options="options"></nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. options: {
  8. autoplay: true,
  9. muted: true,
  10. controls: true
  11. },
  12. });
  13. return { ...toRefs(state) };
  14. }

初始化静音

muted属性设置视频初始化静音

  1. <nut-video :source="source" :options="options"></nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. options: {
  8. muted: true,
  9. controls: true
  10. },
  11. });
  12. return { ...toRefs(state) };
  13. }

视频封面海报设置

poster 属性设置视频海报

  1. <nut-video :source="source" :options="options"></nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. options: {
  8. controls: true,
  9. poster: 'https://img12.360buyimg.com/ling/s345x208_jfs/t1/168105/33/8417/54825/603df06dEfcddc4cb/21f9f5d0a1b3dad4.jpg.webp'
  10. },
  11. });
  12. return { ...toRefs(state) };
  13. }

行内播放

playsinline 属性设置移动端视频行内播放,阻止新打开页面播放(兼容 ios,兼容部分安卓机)

  1. <nut-video :source="source" :options="options"></nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. options: {
  8. playsinline: true,
  9. controls: true,
  10. },
  11. });
  12. return { ...toRefs(state) };
  13. }

视频背景图

当设置视频为背景图时需要将 muted 静音、 disabled 禁止操作、loop 循环播放、autoplay 自动播放设置为 true,移动端需要设置 playsinline 行内展示

  1. <nut-video :source="source" :options="options"></nut-video>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. options: {
  8. controls: false,
  9. autoplay: true,
  10. muted: true,
  11. disabled: true,
  12. playsinline: true,
  13. loop: true
  14. },
  15. });
  16. return { ...toRefs(state) };
  17. }

视频切换

当视频地址发生变化时,重置视频

  1. <nut-video :source="source"></nut-video>
  2. <nut-button type="primary" @click="changeVideo">切换视频</nut-button>
  1. setup() {
  2. const state = reactive({
  3. source: {
  4. src: 'https://storage.jd.com/about/big-final.mp4',
  5. type: 'video/mp4'
  6. },
  7. });
  8. const changeVideo = () => {
  9. state.source1 = {
  10. src: 'https://vjs.zencdn.net/v/oceans.mp4',
  11. type: 'video/mp4'
  12. };
  13. };
  14. return { ...toRefs(state), changeVideo };
  15. }

API

Props

字段说明类型默认值
source视频地址和类型设置Object-
options控制视频播放属性Objectrequired
options.autoplay是否自动播放Booleanfalse
options.poster海报设置String-
options.loop是否循环播放Booleanfalse
options.controls是否展示操作栏Booleantrue
options.muted是否静音Booleanfalse
options.volume音量控制Number0.5
options.disabled禁用操作(如循环播放的背景图,禁止操作)Booleanfalse
options.playsinline是否设置为行内播放元素(解决安卓兼容问题)Booleanfalse

Events

事件名称说明回调参数
play播放
pause暂停
playend播放完成回调

Video  视频播放器 - 图1