WDPlayerRenderView.ets 3.05 KB
import componentUtils from '@ohos.arkui.componentUtils';
import { WDPlayerController } from '../controller/WDPlayerController'
import { WindowModel } from 'wdKit';

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

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

let insIndex: number = 0;

class MGPlayRenderViewIns {
  static intCount: number = 0;

  static add() {
    MGPlayRenderViewIns.intCount++;
    WindowModel.shared.setWindowKeepScreenOn(true);
    console.log("add-- +1")
  }

  static del() {
    console.log("add-- -1")
    MGPlayRenderViewIns.intCount--;
    if (MGPlayRenderViewIns.intCount <= 0) {
      WindowModel.shared.setWindowKeepScreenOn(false);
    }
  }
}

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

  aboutToAppear() {
    MGPlayRenderViewIns.add();

    insIndex++;

    if (!this.playerController) {
      return
    }

    this.playerController.onVideoSizeChange = (width: number, height: number) => {
      // console.log(`WDPlayerRenderView onVideoSizeChange width:${width} videoTop:${height}`)
      this.videoWidth = width;
      this.videoHeight = height;
      this.updateLayout()
    }
  }

  aboutToDisappear() {
    MGPlayRenderViewIns.del();
  }

  build() {
    Row() {
      // 设置为“surface“类型时XComponent组件可以和其他组件一起进行布局和渲染。
      XComponent({
        id: 'xComponentId112233',
        type: 'surface',
        controller: this.xComponentController
      })
        .onLoad(async (event) => {
          this.xComponentController.setXComponentSurfaceSize({
            surfaceWidth: 1920,
            surfaceHeight: 1080
          });
          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);
    if (info.size.width > 0 && info.size.height > 0 && this.videoHeight > 0 && this.videoWidth > 0) {
      if (info.size.width / info.size.height > this.videoWidth / this.videoHeight) {
        let scale = info.size.height / this.videoHeight;
        this.selfSize = new Size((this.videoWidth * scale / info.size.width) * 100 + "%", '100%');
      } else {
        let scale = info.size.width / this.videoWidth;
        this.selfSize = new Size('100%', (this.videoHeight * scale / info.size.height) * 100 + "%");
      }
    }
  }
}