WDPlayerRenderLiveView.ets 4.24 KB
import componentUtils from '@ohos.arkui.componentUtils';
import { WindowModel, Logger } from 'wdKit';
import { enableAliPlayer } from '../utils/GlobalSetting';
import { WDAliPlayerController } from '../controller/WDAliPlayerController';

class Size {
  width: Length = "100%";
  height: Length = "100%";

  constructor(width: Length, height: Length) {
    this.width = width;
    this.height = height;
  }
}

let insIndex: number = 0;
const TAG = 'WDPlayerRenderLiveView'

class MGPlayRenderViewIns {
  static intCount: number = 0;

  static add() {
    MGPlayRenderViewIns.intCount++;
    WindowModel.shared.setWindowKeepScreenOn(true);
    Logger.debug(TAG, "add-- +1")
  }

  static del() {
    Logger.debug(TAG, "del-- -1")
    MGPlayRenderViewIns.intCount--;
    if (MGPlayRenderViewIns.intCount <= 0) {
      WindowModel.shared.setWindowKeepScreenOn(false);
    }
  }
}

/**
 * 播放窗口组件
 */
@Component
export struct WDPlayerRenderLiveView {
  private playerController?: WDAliPlayerController;
  private xComponentController: XComponentController = new XComponentController();
  onLoad?: ((event?: object) => void);
  @State videoWidth: number = 0
  @State videoHeight: number = 0
  @State videoRatio: number = 16 / 9
  @State selfSize: Size = new Size('100%', '100%');
  private insId: string = "WDPlayerRenderLiveView" + insIndex;



  aboutToAppear() {
    MGPlayRenderViewIns.add();
    insIndex++;
    if (!this.playerController) {
      return
    }
    //this.init = true
    this.playerController.onVideoSizeChange = (width: number, height: number) => {
      // console.log(`WDPlayerRenderView onVideoSizeChange width:${width} videoTop:${height}`)
      this.videoWidth = width;
      this.videoHeight = height;
      this.videoRatio = width / height
      this.updateLayout()
    }
  }

  aboutToDisappear() {
    Logger.info(TAG, `aboutToDisappear`)
    MGPlayRenderViewIns.del();
  }

  build() {
    Row() {
      // 设置为“surface“类型时XComponent组件可以和其他组件一起进行布局和渲染。
      XComponent({
        id: enableAliPlayer ? this.insId : 'xComponentId',
        type: XComponentType.SURFACE,
        libraryname: enableAliPlayer ? "premierlibrary" : undefined,
        controller: this.xComponentController
      })
        .onLoad(async (event) => {
          Logger.info(TAG, 'onLoad')
          let surfaceId = this.xComponentController.getXComponentSurfaceId()
          this.xComponentController.setXComponentSurfaceRect({
            surfaceWidth: this.videoWidth,
            surfaceHeight: this.videoHeight
          });
          if (enableAliPlayer) {
            this.playerController?.setSurfaceId(this.insId)
          } else {
            this.playerController?.setXComponentController(this.xComponentController)
          }
          if (this.onLoad) {
            this.onLoad(event)
          }
        })
        // .width(this.selfSize.width)
        // .height(this.selfSize.height)
    }
    .id(this.insId)
    .onAreaChange(() => {
      this.updateLayout()
    })
    .backgroundColor("#000000")
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }

  updateLayout() {

    let info = componentUtils.getRectangleById(this.insId);
    Logger.debug(TAG, "播放器区域变化: " + JSON.stringify(info))

    Logger.debug(TAG, "xComponent rect: " + JSON.stringify(this.xComponentController.getXComponentSurfaceRect()))

    if (info.size.width > 0 && info.size.height > 0) {

      // 竖屏
      if (this.videoHeight > 0 && this.videoWidth > 0 && this.videoWidth < this.videoHeight) {
        let ratio = this.videoWidth / this.videoHeight
        const height = info.size.width / ratio

        // 竖屏,缩放高度大于 视频区域高度
        if (height > info.size.height) {

          Logger.debug(TAG, "ratio = " + ratio + " ==> new height = " + height)

          Logger.debug(TAG, "高度固定,求宽度: " + info.size.height * ratio)

          this.xComponentController.setXComponentSurfaceRect({
            surfaceWidth: info.size.height * ratio,
            surfaceHeight: info.size.height
          });
          return
        }
      }

      this.xComponentController.setXComponentSurfaceRect({
        surfaceWidth: info.size.width,
        surfaceHeight: info.size.height
      });
    }
  }


}