LiveBox.vue 12.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
<template>
  <!-- v-if="LiveProps[liveDetail.status].isLive" -->
  <div class="videoContainer">
    <div class="small-box" @click="isSmallBox = false" v-show="isSmallBox">
      <img class="icon-small-live" src="@/assets/images/icon-small-live.jpg" />
      <div class="flex small-content">
        <!-- <div v-if="LiveStatus[liveDetail.status] == 'live'" class="live-line"> -->
        <div class="live-line" v-if="LiveProps[liveDetail.status].isLive">
          <div class="side"></div>
          <div class="center side"></div>
          <div class="side"></div>
        </div>
        <img v-if="!LiveProps[liveDetail.status].isLive" :src="IMAGES.REPLAY_ICON" class="live-replayIcon" />
        <div class="text">{{ LiveProps[liveDetail.status].status }}</div>
      </div>
    </div>
    <!-- @click="openFullscreen" -->
    <!-- @touchmove.stop.prevent 禁止使用滚动 -->
    <div :style="videoStyle" id="live-box" :class="['live-box', { videoFullscreen: videoFullscreen }]" v-show="!isSmallBox">
      <div class="sprite-icon icon-live-close" v-show="!videoFullscreen && !isSmallBox" @click="isSmallBox = true"></div>

      <div :class="{ videoShort: isAction }">
        <div class="mask">
          <div class="status flex">
            <div v-if="LiveProps[liveDetail.status].isLive" class="live-line">
              <div class="side"></div>
              <div class="center side"></div>
              <div class="side"></div>
            </div>
            <!-- 回放图标 -->
            <img v-if="!LiveProps[liveDetail.status].isLive" :src="IMAGES.REPLAY_ICON" class="live-replayIcon" />
            <div class="text">{{ LiveProps[liveDetail.status].text }}</div>
          </div>
          <template v-if="isShowGif">
            <img class="coverGif" :src="detail[LiveProps[liveDetail.status].gifAttr] || LiveProps[liveDetail.status].covergif" alt="" />
          </template>
        </div>
        <div class="fullscreen-btn" @click="openFullscreen(false)">
          <img :src="IMAGES.FULL_SCREEN" />
        </div>
        <div v-if="detail.groupQrCode" class="poster-btn" @click="showKf = true">
          <img :src="detail.floatingMaskDiagram" />
        </div>
        <video id="player-container-id" preload="auto" playsinline webkit-playsinline></video>
        <group-modal v-model="showKf" :detail="detail" />
      </div>

      <div v-show="videoFullscreen">
        <!-- IM -->
        <im v-if="LiveProps[liveDetail.status].isLive && !mediaLive" :liveDetail="liveDetail" :isAction="isAction" :detail="detail" class="Im"></im>
        <action :videoFullscreen="true" class="BgAction" v-show="isAction && (!LiveProps[liveDetail.status].isLive|| mediaLive)"></action>
      </div>
    </div>
  </div>
</template>
<script type="text/ecmascript-6">
import { wxConfig } from '@/api'
const IMAGES = Object.freeze({
  // COVER_LIVE: 'https://wimg.588ku.com/gif620/20/04/23/79a7882dfb0c59497976fd8b3bfe60ef.gif',
  COVER_LIVE: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/COVER_LIVE.gif',
  COVER_REPLAY: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/COVER_REPLAY.gif',
  REPLAY_ICON: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/replayIcon.png',
  FULL_SCREEN: 'https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/fullscreen.png',
  GROUP: "https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/gift.gif"
})
const TCPlayerConfig = Object.freeze({
  autoplay: true, // 自动播放
  // loop: false, // 循环播放
  muted: true, // 是否静音
  controlBar: { // 控制条配置
    fullscreenToggle: false // 隐藏全屏按钮
  },
  webrtcConfig: { // webrtcConfig 参数列表
    showLog: true // 打印webrtc日志
  }
})
const LiveProps = Object.freeze({
  '0': { isShowBox: false },
  '1': { status: '直播', text: '正在直播', isLive: true, covergif: IMAGES.COVER_LIVE, isShowBox: true, gifAttr: 'liveBroadcastRoomCover' },
  '2': { status: '回放', text: '直播回放', isLive: false, covergif: IMAGES.COVER_REPLAY, isShowBox: true, icon: IMAGES.REPLAY_ICON, gifAttr: 'liveBroadcastRoomEndCover' }
})
const PLAYERDOMID = 'player-container-id'
export default {
  name: 'LiveBox',
  data() {
    return {
      player: null,
      videoFullscreen: false,
      showKf: false,
      isShowGif: false,
      videoStyle: '',
      isSmallBox: false, //是否显示小容器直播
      LiveProps,
      IMAGES,
      fileIdx: 0
    }
  },
  computed: {
    isAction() {
      const { activityStatus, buyFlag } = this.detail
      return activityStatus != 3 && buyFlag != 2 //TODO CONSTANT
    }
  },
  props: {
    detail: {
      type: Object,
      default: () => { }
    },
    liveDetail: {
      type: Object,
      default: () => {
        return {
          // 直播状态 0:未开始;1:直播中;2:已结束;
          status: 1,
          // 直播播放地址(status=1生效)
          playPath: '',
          // 回放列表
          liveBroadcastRecordingFileList: []
        }
      }
    },
    mediaLive:Boolean
  },
  mounted() {
    this.createPlayerCover()
    this.createPlayer()
    this.play()
  },
  methods: {
    createPlayerCover() {
      !this.IsIOS && (this.isShowGif = true)
    },
    createPlayer() {
      this.player = TCPlayer(PLAYERDOMID, TCPlayerConfig)
    },
    play() {
      wxConfig.ready(() => {
        wx.getNetworkType({
          success: (result) => {
            // console.log('====', { result });
            this.player.ready(() => { // 设置播放器初始化完成后的回调
              this.fixHls()
              const _this = this
              const src =  _this.mediaLive ? this.liveDetail.liveBroadcastMediaUrl : this.LiveProps[this.liveDetail.status].isLive ? this.liveDetail.pullWebRtcUrl : (this.liveDetail.liveBroadcastRecordingFileList || [])[this.fileIdx]?.videoUrl
              this.player.src(src)
              this.player.poster()
              this.player.play() // 播放视频
              // 录播,视频播放ended
              this.player.on('ended', function (event) {
                let fileList = _this.liveDetail.liveBroadcastRecordingFileList
                if(fileList.length == 1) {
                  _this.player.play()
                  return true
                }
                // 从回调参数 event 中获取事件状态码及相关数据
                _this.fileIdx = _this.fileIdx == (fileList.length - 1) ? 0 : _this.fileIdx + 1
                _this.player.src(fileList[_this.fileIdx]?.videoUrl)
                _this.player.play()
              })
              this.draggable(document.getElementById('live-box'), this.openFullscreen, 'icon-live-close')
            })
          }
        })
      })
    },

    controlBarStyle() {
      var [controlBar] = document.getElementsByClassName('vjs-control-bar')
      controlBar.style.bottom = this.isAction ? '10vh' : '0'
      controlBar.style.display = this.LiveProps[this.liveDetail.status].isLive ? 'none' : 'auto'
    },

    fixHls() {
      if (Hls.isSupported()) {
        this.video = document.getElementById(PLAYERDOMID)
        this.hls = new Hls()
        // bind them together
        this.hls.attachMedia(this.video)
        this.dealTcPlayerErr14()
      }
    },
    // 打开关闭全屏
    openFullscreen(status = true) {
      // console.log('openFullscreen')
      this.isShowGif = false
      const [live] = document.getElementsByClassName('live-box')
      const [volumePanel] = document.getElementsByClassName('vjs-volume-panel ')
      const [volume] = volumePanel.getElementsByClassName('vjs-mute-control'),
        [body] = document.getElementsByTagName('body'),
        bodyClass = body.className
      volume.click()
      this.player.play()
      this.videoFullscreen = status
      live.id = status ? '' : 'live-box'
      body.className = status ? 'van-overflow-hidden' : bodyClass.replace('van-overflow-hidden', '')
      this.controlBarStyle()
      this.$emit('handleLiveFull', { isFull: this.videoFullscreen })
    },
    // 处理tcplayer错误码14
    dealTcPlayerErr14() {
      this.hls.on(Hls.Events.ERROR, function (event, data) {
        if (data.fatal) {
          switch (data.type) {
            case Hls.ErrorTypes.NETWORK_ERROR:
              // try to recover network error
              // console.log('fatal network error encountered, try to recover')
              this.hls.startLoad()
              break
            case Hls.ErrorTypes.MEDIA_ERROR:
              // console.log('fatal media error encountered, try to recover')
              this.hls.recoverMediaError()
              break
            default:
              // cannot recover
              this.hls.destroy()
              break
          }
        }
      })
    },
    changeGifStatus() {
      this.isShowGif = !this.isShowGif
    },
    continueVideo() {
      this.player.play()
    }
  }
}
</script>
<style scoped lang="scss">
.small-box {
  position: fixed;
  top: 10%;
  right: 0;
  z-index: 7;
  // position: relative;

  width: 52px;
  height: 21px;
  font-size: 12px;
  font-weight: 600;
  color: #ffffff;
  .icon-small-live {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 10px 0 0 10px;
    // object-fit: cover;
  }
  .small-content {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 9;
    line-height: 21px;
    // padding-left: 5px;
    // padding: ;
    justify-content: center;
    // overflow: hidden;
    border-radius: 0 0 10px 10px;
    // padding: ;
    text-align: right;
    .live-replayIcon {
      position: absolute;
      left: 7px;
      top: 5px;
      display: inline-block;
      width: 10px;
      height: 10px;
      z-index: 9;
    }
    .text {
      z-index: 40;
      margin-left: 15px;
      font-size: 12px;
    }
  }
}
.live-box {
  box-sizing: border-box;
  position: fixed;
  top: 23%;
  left: calc(98% - 86px);
  z-index: 7;
  width: 82.5px;
  height: 150px;
  border-radius: 4px;
  overflow: hidden;
  user-select: auto;
  // transition: width 0.1s ease, height 0.1s ease;
  .icon-live-close {
    position: absolute;
    top: 2px;
    right: 2px;
    // left: -100px;
    z-index: 3;
  }
  .mask {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
    border: 2px solid #f95d35;
    border-radius: 4px;
    box-sizing: border-box;

    .coverGif {
      object-fit: contain;
      height: 126px;
    }
    .status {
      position: absolute;
      left: -2px;
      bottom: -2px;
      width: 104%;
      height: 18px;
      background: linear-gradient(291deg, #f6542b 0%, #ff6c47 100%), linear-gradient(270deg, rgba(255, 57, 0, 0) 0%, #ff3900 33%, #fd466f 100%);
      font-size: 12px;
      font-weight: 500;
      color: #ffffff;
      box-sizing: border-box;
      padding: 0 10px;
      .live-replayIcon {
        display: inline-block;
        width: 10px;
        height: 10px;
      }
      .text {
        margin-left: auto;
      }
    }
  }
  #player-container-id {
    width: 100%;
    height: 100%;
    video {
      object-fit: cover !important;
    }
  }
  .fullscreen-btn {
    display: none;
    position: absolute;
    left: 12px;
    top: 18px;
    width: 54px;
    height: 48px;
    z-index: 2;
    animation: xing ease-in-out 2s infinite;
  }
  @keyframes xing {
    0% {
      transform: scale(0.85);
    }

    25% {
      transform: scale(1);
    }

    50% {
      transform: scale(0.85);
    }

    75% {
      transform: scale(1);
    }

    100% {
      transform: scale(0.85);
    }
  }
  .poster-btn {
    display: none;
    position: absolute;
    right: 12px;
    top: 18px;
    width: 60px;
    height: 88px;
    z-index: 2;
  }
  /deep/.vjs-control-bar {
    opacity: 0;
  }
}
.videoFullscreen {
  position: relative;
  width: 100vw !important;
  height: 100vh !important;
  left: 0 !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  border: none !important;
  background: #000000;
  overflow-y: hidden;
  .videoShort {
    // height: calc(100vh - 13.333vh) !important;
    height: 100%;
  }
  .mask {
    display: none;
  }
  .fullscreen-btn,
  .poster-btn {
    display: block;
  }

  /deep/.vjs-control-bar {
    opacity: 1 !important;
    // bottom: 10vh;
    background: none !important;
    // background-color: rgba(0, 0, 0, 0.3);
  }
  .BgAction {
    // 80%,80%, rgba(0, 0, 0, 0.4),
    background: linear-gradient(0deg, rgba(0, 0, 0, 0.6), rgba(225, 225, 225, 0)); /* 标准的语法(必须放在最后) */
  }
}
.Im {
  position: absolute;
  bottom: 10px;
  left: 0;
  width: 100%;
}
</style>