PlayerComponent.ets 3.17 KB
import { LiveDetailsBean } from 'wdBean/Index';
import { WDPlayerController, WDPlayerRenderVLiveView, WDPlayerRenderView } from 'wdPlayer/Index';
import componentUtils from '@ohos.arkui.componentUtils';


@Component
export struct PlayerComponent {
  private playerController?: WDPlayerController;
  // playerController: WDPlayerController = new WDPlayerController();
  @Consume @Watch('updateData') liveDetailsBean: LiveDetailsBean
  @State bottomSafeHeight: number = AppStorage.get<number>('bottomSafeHeight') || 0
  @State topSafeHeight: number = AppStorage.get<number>('topSafeHeight') || 0
  @State imgUrl: string = ''
  @State isWait: boolean = false
  @State liveStreamType: number | null = -1
  @State playUrl: string = ''

  aboutToAppear(): void {
    if (this.playerController) {
      this.playerController.onCanplay = () => {
        console.log('可以播放了')
        this.playerController?.play()
        // this.playerController.selfSize
      }
    }

  }

  aboutToDisappear(): void {
    this.playerController?.pause()
    this.playerController?.stop()
    this.playerController?.release()
  }

  updateData() {
    console.error('updateData=============')
    //直播新闻-直播状态  wait待开播running直播中end已结束cancel已取消paused暂停
    if (this.liveDetailsBean.fullColumnImgUrls && this.liveDetailsBean.fullColumnImgUrls.length > 0) {
      this.imgUrl = this.liveDetailsBean.fullColumnImgUrls[0].url
    }
    this.isWait = this.liveDetailsBean?.liveInfo?.liveState == 'wait'
    if (this.liveDetailsBean.liveInfo && this.liveDetailsBean.liveInfo.vlive.length > 0) {
      let playUrl = ''
      let liveStreamType: number | null = null
      if (this.liveDetailsBean.liveInfo.liveState == 'running') {
        playUrl = this.liveDetailsBean.liveInfo.vlive[0].liveUrl
        liveStreamType = this.liveDetailsBean.liveInfo.vlive[0].liveStreamType
      } else if (this.liveDetailsBean.liveInfo.liveState == 'end') {
        playUrl = this.liveDetailsBean.liveInfo.vlive[0].replayUri
        liveStreamType = this.liveDetailsBean.liveInfo.vlive[0].liveStreamType
      }

      this.liveStreamType = liveStreamType
      this.playUrl = playUrl

    }
  }

  build() {
    Column() {
      Stack() {
        Image(this.imgUrl)
          .height('100%')
          .width('100%')
          .blur(100)
          .renderFit(RenderFit.RESIZE_COVER)

        // TODO:判断横竖屏,liveStreamType=1竖屏铺满屏幕,liveStreamType=0横屏正常展示
        if (this.liveStreamType == null) {
          WDPlayerRenderVLiveView({
            playerController: this.playerController,
            onLoad: () => {
              this.playerController?.firstPlay(this.playUrl);
            }
          }).height('100%')
            .width('100%')
        } else if (this.liveStreamType == 0) {
          WDPlayerRenderView({
            playerController: this.playerController,
            onLoad: () => {
              this.playerController?.firstPlay(this.playUrl);
            }
          }).margin({ top: 195 }).height(211)
        }
      }
      .height('100%')
      .width('100%')
      .align(Alignment.Top)
      .alignContent(Alignment.Top)
    }
    .height('100%')
    .width('100%')
  }
}