PlayerProgressView.ets 2.68 KB
import { ContentDetailDTO } from 'wdBean/Index';
import { DateTimeUtils } from 'wdKit/Index';
import { PlayerConstants, WDPlayerController } from 'wdPlayer/Index';

@Component
export struct PlayerProgressView {
  private playerController?: WDPlayerController;
  @Consume contentDetailData: ContentDetailDTO
  @Consume progressVal: number;
  @Consume isOpenDetail: boolean
  @Consume isDragging: boolean
  @State status: number = PlayerConstants.STATUS_START;
  @State videoDuration: number = this.contentDetailData?.videoInfo?.[0]?.videoDuration || 1

  aboutToAppear() {
    if (this.playerController) {
      this.playerController.onStatusChange = (status: number) => {
        this.status = status
      }
      this.playerController.onSeekDone = (status: number) => {
        this.playerController?.play()
      }
    }
  }

  build() {
    Column() {
      Text() {
        Span(DateTimeUtils.secondToTime(Math.floor(this.progressVal / 100 * this.videoDuration)))
        Span(' / ')
        Span(DateTimeUtils.secondToTime(this.videoDuration))
      }
      .fontSize(24)
      .fontColor(Color.White)
      .fontWeight(600)
      .margin({ bottom: 30 })
      .visibility(this.isDragging ? Visibility.Visible : Visibility.None)

      Slider({
        value: this.progressVal,
        step: 0.01,
        // style: SliderStyle.OutSet
      })
        .blockColor(this.status === PlayerConstants.STATUS_PAUSE || this.isDragging ? $r('app.color.play_block_color') :
        Color.Transparent)
        .trackColor(this.status === PlayerConstants.STATUS_PAUSE || this.isDragging ?
        $r('app.color.pause_track_color') : $r('app.color.play_track_color'))
        .selectedColor(this.status === PlayerConstants.STATUS_PAUSE || this.isDragging ?
        $r('app.color.pause_selected_color') : $r('app.color.play_selected_color'))
        .trackThickness(this.status === PlayerConstants.STATUS_PAUSE || this.isDragging ? 4 : 1)
        .blockStyle({
          type: this.status === PlayerConstants.STATUS_PAUSE || this.isDragging ? SliderBlockType.IMAGE :
          SliderBlockType.DEFAULT,
          image: $r('app.media.ic_player_block')
        })
        .blockSize({ width: 18, height: 12 })
        .width('100%')
        .height(19)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.progressVal = value
          if (mode === SliderChangeMode.Moving) {
            this.isDragging = true
          }
          if (mode === SliderChangeMode.End) {
            this.isDragging = false
          }
          this.playerController?.setSeekTime(value, mode);
          console.log('slider onChange:', value, mode)

        })
    }
    .visibility(this.isOpenDetail ? Visibility.None : Visibility.Visible)
  }
}