vue3使用video-player实现视频播放(可拖动视频窗口、调整大小)

1.安装video-player

复制代码
npm install video.js @videojs-player/vue --save

在main.js中配置全局引入

复制代码
// 导入视频播放组件
import VueVideoPlayer from '@videojs-player/vue'
import 'video.js/dist/video-js.css'


const app = createApp(App)
// 视频播放组件
app.use(VueVideoPlayer)

2.安装拖拽组件**vue3-draggable-resizable**

复制代码
npm install vue3-draggable-resizable

3.使用**vue3-draggable-resizable**

复制代码
<template>
  <div id="app">
    <div class="parent">
      <Vue3DraggableResizable
        :initW="110"
        :initH="120"
        v-model:x="x"
        v-model:y="y"
        v-model:w="w"
        v-model:h="h"
        v-model:active="active"
        :draggable="true"
        :resizable="true"
        @activated="print('activated')"
        @deactivated="print('deactivated')"
        @drag-start="print('drag-start')"
        @resize-start="print('resize-start')"
        @dragging="print('dragging')"
        @resizing="print('resizing')"
        @drag-end="print('drag-end')"
        @resize-end="print('resize-end')"
      >
        This is a test example
      </Vue3DraggableResizable>
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'
export default defineComponent({
  components: { Vue3DraggableResizable },
  data() {
    return {
      x: 100,
      y: 100,
      h: 100,
      w: 100,
      active: false
    }
  },
  methods: {
    print(val) {
      console.log(val)
    }
  }
})
</script>
<style>
.parent {
  width: 200px;
  height: 200px;
  position: absolute;
  top: 100px;
  left: 100px;
  border: 1px solid #000;
  user-select: none;
}
</style>

具体使用方法可查看官方文档:vue3-draggable-resizable/docs/document_zh.md at main · a7650/vue3-draggable-resizable · GitHub

首先去掉右上角的缩放句柄,将一个按钮放在右上角,这样我们就可以随时使用v-if关闭这个拖拽窗口,将视频放入拖拽窗口中,代码:

复制代码
<Vue3DraggableResizable v-if="cunzai" :lockAspectRatio="true"
        :handles="['tl', 'tm', 'mr', 'ml', 'bl', 'bm', 'br']" :initW="320" :initH="180" :minW="320" :minH="180"
        v-model:x="x" v-model:y="y" v-model:w="w" v-model:h="h" v-model:active="active" :draggable="true"
        :resizable="true" :parent="true" @activated="print('activated')" @deactivated="print('deactivated')"
        @drag-start="print('drag-start')" @resize-start="print('resize-start')" @dragging="print('dragging')"
        @resizing="print('resizing')" @drag-end="print('drag-end')" @resize-end="print('resize-end')">
        <div class="close" @click="close()">
          <Icon size="20" color="#1196db" type="md-close-circle" />
        </div>

        <div>
          <video-player v-if="modal1" :controls="true" :autoplay="false" :src="videoSrc" :options="playerOptions"
            :volume="0.6" />
        </div>
      </Vue3DraggableResizable>

视频部分代码:

复制代码
// 视频链接地址
const videoSrc = ref('url');  //自定义url
// 视频播放器配置
let playerOptions = ref({
  height: 250,
  width: 250, 
  playbackRates: [0.7, 1.0, 1.5, 2.0], 
  autoplay: false, 
  muted: true, 
  loop: true, 
  preload: 'auto', 
  language: 'zh-CN',
  aspectRatio: '16:9', 
  fluid: true, 
  notSupportedMessage: '此视频暂无法播放,请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
  controls: true,
  controlBar: {
    timeDivider: true,
    durationDisplay: true,
    remainingTimeDisplay: false,
    fullscreenToggle: true // 全屏按钮
  }
})
const cunzai = ref(false);
//关闭视频
function close() {
  cunzai.value = false
}

这样就可以实现视频窗口的放大缩小移动。

放大移动;

相关推荐
FungLeo7 小时前
成为全栈·React 管理后台篇·总复盘:从“接口能调通”到“后台值得使用”
前端·react.js·前端框架·项目复盘·成为全栈
allnlei7 小时前
Opus In-Band FEC 详解:原理、实现与开源方案
ffmpeg·音视频·gstreamer
爱吃提升8 小时前
文生视频模型发展趋势(2026)
人工智能·音视频
Csvn8 小时前
并发模式:让渲染学会排队、插队和让路
前端
可乐鸡翅yeah_9 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
Flynt9 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
JavaGuide10 小时前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel10 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
apple_6926588510 小时前
最新ComfyUI 0.37.1使用教程:画图、3D、音频都有改动 ComfyUI升级到0.37.1啦,新增或改进五大功能
音视频
莪_幻尘11 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm