利用西瓜播放器(xgplayer)实现直播点播

刁茂才
2023-12-01
<template>
  <div class="pc-live">
    <div class="left">
      <!-- 开启声音 -->
      <div class="sound" @click.stop="openSound" v-if="sound">
        <img src="./../assets/live-jingyin.png" alt="" />
        <span class="word">取消静音</span>
      </div>
      <div class="live-player">
        <div class="yes-start" id="mse" v-if="liveStatus"></div>
        <div class="no-start" v-else>
          <img src="./../assets/poster-live.png" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Player from 'xgplayer';
import HlsJsPlayerAll from 'xgplayer-hls.js'; // hls完整版
import FlvJsPlayer from 'xgplayer-flv.js';

export default {
  name: 'live-index',
  components: {
    // VueScroll
  },
  data () {
    return {
      // 开启声音控件
      sound: false,

      xgplayer: null,
      liveStatus: 0, // 直播状态
      liveUrl: '', // 直播的链接
      btnStatus: false, // 解决按钮快速点击重复提交 true为提交中 false 可以再次提交
    }
  },
  created () {
    localStorage.setItem('dplayer-volume', 0)
  },
  mounted () {
    this.getliveComment()
  },
  methods: {
    // 开启声音
    openSound () {
      this.xgplayer.video.volume = 0.8
      this.sound = false
    },
    // 获取直播流
    getliveComment () {
      const config = this.$store.state.config

      this.liveStatus = parseInt(config.liveStatus.val)
      
      if (this.liveStatus != 0) {
        this.sound = true
        // 是否Flv
        const wayFlv = (config.liveUrl.remark.indexOf('flv') != -1)
        if(wayFlv){
          this.liveUrl = config.liveUrl.remark
          this.$nextTick(() => {
            this.dpInitFlv()
          })
          return
        }
        // 是否Hls
        const wayHls = (config.liveUrl.remark.indexOf("m3u8") != -1)
        if(wayHls){
          this.liveUrl = config.liveUrl.remark
          this.$nextTick(() => {
            this.dpInitHls()
          })
          return
        }
        // 是否MP4
        const wayMp4 = (config.liveUrl.remark.indexOf("mp4") != -1)
        if(wayMp4){
          this.liveUrl = config.liveUrl.remark
          this.$nextTick(() => {
            this.dpInitMp4()
          })
          return
        }
      }
    },

    // 用于播放Flv
    dpInitFlv () {
      if (this.xgplayer) return
      this.xgplayer = new FlvJsPlayer({
        id: 'mse',
        poster: require('./../assets/poster-live.png'),
        url: this.liveUrl,
        width: 1400,
        height: 780,
        fitVideoSize: 'fixWidth',
        errorTips: `播放失败,请<span>刷新</span>重试`,
        isLive: true,
        // preloadTime: 0, // 预加载时长(秒)
        // minCachedTime: 5,
        volume: 0,
        // autoplayMuted: true,  // 用了取消禁音会失效  
        autoplay: true,
        cors: true
      })
    },
    // 用于播放Hls
    dpInitHls () {
      if (this.xgplayer) return
      this.xgplayer = new HlsJsPlayerAll({
        id: 'mse',
        poster: require('./../assets/poster-live.png'),
        url: this.liveUrl,
        width: 1400,
        height: 780,
        fitVideoSize: 'fixWidth',
        errorTips: `播放失败,请<span>刷新</span>重试`,
        isLive: true,
        playsinline: true,
        volume: 0,
        // autoplayMuted: true,   
        autoplay: true,
        cors: true,
        ignores: ['time', 'progress']
      })
    },
    // 用于回播
    dpInitMp4 () {
      if (this.xgplayer) return
      this.xgplayer = new Player({
        id: 'mse',
        poster: require('./../assets/poster-live.png'),
        url: this.liveUrl,
        width: 1400,
        height: 780,
        fitVideoSize: 'fixWidth',
        errorTips: `播放失败,请<span>刷新</span>重试`,
        volume: 0,
        // autoplayMuted: true,
        autoplay: true,
      })
    },
  },
  beforeDestroy () {
  }
}
</script>

<style lang="scss" scoped>
.pc-live {
  width: 100vw;
  min-height: 100vh;
  background: url("./../assets/bg-pclive.png") no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .reload {
    margin: 40px auto 30px;

    span {
      display: block;
      width: 100px;
      height: 35px;
      margin: 0 auto;
      line-height: 35px;
      font-size: 18px;
      font-weight: 700;
      text-align: center;
      background-color: #fff;
      border-radius: 4px;
      cursor: pointer;
    }
  }

  .left {
    margin: 100px auto 0;
    width: 1400px;
    position: relative;

    .sound {
      width: 150px;
      height: 35px;
      line-height: 35px;
      text-align: center;
      background-color: #fff;
      border-radius: 4px;
      z-index: 999;
      position: absolute;
      left: 20px;
      top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      img {
        width: 20px;
        margin-right: 5px;
      }

      .word {
        font-weight: 700;
        font-size: 18px;
      }
    }

    .live-player {
      width: 1400px;

      .yes-start {
        width: 100%;
        height: 100%;
      }

      .no-start {
        img {
          display: block;
          width: 1400px;
        }
      }
    }
  }
}
</style>

 类似资料: