vue使用西瓜视频插件xgplayer

Vue2

npm install xgplayer-vue@latest

引入xgplayer,封装成一个公共组件

src/components/xgPlayer/xgPlayer.vue

javascript 复制代码
<template>
  <Xgplayer :config="config" @player="Player = $event" />
</template>

<script>
import Xgplayer from 'xgplayer-vue'
export default {
  components: {
    Xgplayer
  },
  data () {
    return {
      config: {
        id: 'vs',
        url: 'https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4', // 修改为能播放的视频地址
        width: '50%', // 宽度可以是 数字 ,也可以是百分比
        height: 500,
        // autoplay: true,
        download: true,
        poster: 'http://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/poster.jpg',
        playbackRate: [0.5, 0.75, 1, 1.5, 2] // 倍速
      },
      Player: null
    }
  }
}

</script>

<style>
#xgPlayer{
  border: 1px solid red;
}
</style>

组件中使用

javascript 复制代码
<template>
  <div>
    <h1>播放器</h1>
    <Player  />
  </div>
</template>

<script>
import Player from '@/components/xgPlayer/xgPlayer.vue'
export default {
  name: 'chartContainer',
  components: {
    Player
  },
</script>

<style>

</style>

Vue3

javascript 复制代码
<template>
  <div style="width: 100%; height: 100%"
       :id="id"></div>
</template>
<script setup>
import { onMounted, watch } from 'vue'
import Player from 'xgplayer'

const props = defineProps({
  id: {
    type: String,
    required: true,
  },
  videoUrl: {
    type: String,
    default: () =>
      'https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-360p.mp4',
  },
  poster: {
    type: String,
    default: () =>
      'http://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/poster.jpg',
  },
  playsinline: {
    type: Boolean,
    default: true,
  },
  width: {
    type: String,
    default: '50%',
  },
  height: {
    type: String,
    default: '50%',
  },
})

watch(
  () => props.videoUrl,
  (newUrl) => {
    initPlayer()
  },
  {
    deep: true,
  }
)

onMounted(() => {
  initPlayer()
})

// 初始化西瓜视频
const initPlayer = () => {
  let player = new Player({
    id: props.id,
    url: props.videoUrl,
    poster: props.poster,
    playsinline: props.playsinline,
    height: props.height,
    width: props.width,
    thumbnail: {
      pic_num: 44,
      width: 160,
      height: 90,
      col: 10,
      row: 10,
      urls: [
        '//lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/byted-player-videos/1.0.0/xgplayer-demo-thumbnail.jpg',
      ],
    },
    danmu: {
      comments: [
        {
          duration: 15000,
          id: '1',
          start: 3000,
          txt: '长弹幕长弹幕长弹幕长弹幕长弹幕',
          style: {
            //弹幕自定义样式
            color: '#ff9500',
            fontSize: '20px',
            border: 'solid 1px #ff9500',
            borderRadius: '50px',
            padding: '5px 11px',
            backgroundColor: 'rgba(255, 255, 255, 0.1)',
          },
        },
      ],
      area: {
        start: 0,
        end: 1,
      },
    },
    whitelist: [''],
  })
  //  超清、高清、标清 分别对应的地址
  player.emit('resourceReady', [
    {
      name: '超清',
      url: '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4',
    },
    {
      name: '高清',
      url: '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4',
    },
    {
      name: '标清',
      url: '//sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/mp4/xgplayer-demo-720p.mp4',
    }
  ])
}
</script>
javascript 复制代码
<template>

  <div class="bigBox">
    <h1>播放器</h1>
    <XgPlayer :id="'xgPlayer'" />
  </div>

</template>


<script lang="ts" setup>
import XgPlayer from '@/components/xgPlayer/xgPlayer.vue'
import 'xgplayer/dist/index.min.css'
</script>


<style scoped>
.bigBox{
  height: 95vh !important;
}
</style>
相关推荐
华仔啊1 小时前
jQuery 4.0 发布,IE 终于被放弃了
前端·javascript
一心赚狗粮的宇叔1 小时前
03.Node.js依赖包补充说明及React&Node.Js项目
前端·react.js·node.js
子春一1 小时前
Flutter for OpenHarmony:音律尺 - 基于Flutter的Web友好型节拍器开发与节奏可视化实现
前端·flutter
JarvanMo1 小时前
150万开发者“被偷家”!这两款浓眉大眼的 VS Code 插件竟然是间谍
前端
亿元程序员1 小时前
大佬,现在AI游戏开发教程那么多,你不搞点卖给大学生吗?
前端
未来龙皇小蓝1 小时前
RBAC前端架构-02:集成Vue Router、Vuex和Axios实现基本认证实现
前端·vue.js·架构
空白诗1 小时前
高级进阶 React Native 鸿蒙跨平台开发:slider 滑块组件 - 进度条与评分系统
javascript·react native·react.js
晓得迷路了1 小时前
栗子前端技术周刊第 116 期 - 2025 JS 状态调查结果、Babel 7.29.0、Vue Router 5...
前端·javascript·vue.js
How_doyou_do1 小时前
执行上下文、作用域、闭包 patch
javascript
叫我一声阿雷吧2 小时前
深入理解JavaScript作用域和闭包,解决变量访问问题
开发语言·javascript·ecmascript