From 4f6b02b67bd8c25ddb13d4d9a395be28475ba63f Mon Sep 17 00:00:00 2001 From: rekirt Date: Thu, 12 Jun 2025 14:31:57 +0800 Subject: [PATCH] fix problems Signed-off-by: rekirt --- entry/src/main/ets/pages/VideoPlay.ets | 118 ++++++++++++++----------- entry/src/main/ets/pages/WindowPip.ets | 2 +- 2 files changed, 67 insertions(+), 53 deletions(-) diff --git a/entry/src/main/ets/pages/VideoPlay.ets b/entry/src/main/ets/pages/VideoPlay.ets index d301e77..443b9e5 100644 --- a/entry/src/main/ets/pages/VideoPlay.ets +++ b/entry/src/main/ets/pages/VideoPlay.ets @@ -70,7 +70,7 @@ export struct PlayVideo { @State hintMsgVisibility: boolean = false; private mXComponentController = new XComponentController(); private surfaceId = ''; - private navigationId: string = ''; + navigationId: string = ''; private player?: AVPlayer; private pipController: PiPWindow.PiPController | undefined = undefined; private eventHub = getContext().eventHub; @@ -93,8 +93,10 @@ export struct PlayVideo { this.player = undefined; } this.eventHub?.off('onStateChange'); - this.pipController?.off('stateChange'); - this.pipController?.off('controlPanelActionEvent'); + if (canIUse('SystemCapability.Window.SessionManager')) { + this.pipController?.off('stateChange'); + this.pipController?.off('controlPanelActionEvent'); + } } async startPip(): Promise { @@ -105,7 +107,9 @@ export struct PlayVideo { Logger.info(`[${TAG}] pipController create error`); return; } - await this.pipController.startPiP(); + if (canIUse('SystemCapability.Window.SessionManager')) { + await this.pipController.startPiP(); + } } async stopPip(): Promise { @@ -113,65 +117,73 @@ export struct PlayVideo { Logger.info(`[${TAG}] pipController is not exist`); return; } - await this.pipController.stopPiP(); + if (canIUse('SystemCapability.Window.SessionManager')) { + await this.pipController.stopPiP(); + } } async createPipController(): Promise { - this.pipController = await PiPWindow.create({ - context: getContext(this), - componentController: this.mXComponentController, - navigationId: this.navigationId, - templateType: PiPWindow.PiPTemplateType.VIDEO_PLAY, - contentWidth: this.curBp === 'sm' ? 16 : this.curBp === 'md' ? 16 : 3, - contentHeight: this.curBp === 'sm' ? 9 : this.curBp === 'md' ? 9 : 2, - }); - this.pipController.on('stateChange', (state: PiPWindow.PiPState, reason: string) => { - this.onStateChange(state, reason); - }); - this.pipController.on('controlPanelActionEvent', (event: PiPWindow.PiPActionEventType, status?: number) => { - this.onActionEvent(event, status); - }); + if (canIUse('SystemCapability.Window.SessionManager')) { + this.pipController = await PiPWindow.create({ + context: getContext(this), + componentController: this.mXComponentController, + navigationId: this.navigationId, + templateType: PiPWindow.PiPTemplateType.VIDEO_PLAY, + contentWidth: this.curBp === 'sm' ? 16 : this.curBp === 'md' ? 16 : 3, + contentHeight: this.curBp === 'sm' ? 9 : this.curBp === 'md' ? 9 : 2, + }); + this.pipController.on('stateChange', (state: PiPWindow.PiPState, reason: string) => { + this.onStateChange(state, reason); + }); + this.pipController.on('controlPanelActionEvent', (event: PiPWindow.PiPActionEventType, status?: number) => { + this.onActionEvent(event, status); + }); + } } destroyPipController(): void { if (!this.pipController) { return; } - this.pipController.off('stateChange'); - this.pipController.off('controlPanelActionEvent'); - this.pipController = undefined; + if (canIUse('SystemCapability.Window.SessionManager')) { + this.pipController.off('stateChange'); + this.pipController.off('controlPanelActionEvent'); + this.pipController = undefined; + } } onStateChange(state: PiPWindow.PiPState, reason: string): void { - switch (state) { - case PiPWindow.PiPState.ABOUT_TO_START: - this.curState = 'ABOUT_TO_START'; - this.curError = $r('app.string.current_error_hint'); - break; - case PiPWindow.PiPState.STARTED: - this.curState = 'STARTED'; - this.curError = $r('app.string.current_error_hint'); - break; - case PiPWindow.PiPState.ABOUT_TO_STOP: - this.curState = 'ABOUT_TO_STOP'; - this.curError = $r('app.string.current_error_hint'); - break; - case PiPWindow.PiPState.STOPPED: - this.player?.updatePlayStatus(true); - this.player?.play(); - this.curState = 'STOPPED'; - this.curError = $r('app.string.current_error_hint'); - break; - case PiPWindow.PiPState.ABOUT_TO_RESTORE: - this.curState = 'ABOUT_TO_RESTORE'; - this.curError = $r('app.string.current_error_hint'); - break; - case PiPWindow.PiPState.ERROR: - this.curState = 'ERROR'; - this.curError = reason; - break; - default: - break; + if (canIUse('SystemCapability.Window.SessionManager')) { + switch (state) { + case PiPWindow.PiPState.ABOUT_TO_START: + this.curState = 'ABOUT_TO_START'; + this.curError = $r('app.string.current_error_hint'); + break; + case PiPWindow.PiPState.STARTED: + this.curState = 'STARTED'; + this.curError = $r('app.string.current_error_hint'); + break; + case PiPWindow.PiPState.ABOUT_TO_STOP: + this.curState = 'ABOUT_TO_STOP'; + this.curError = $r('app.string.current_error_hint'); + break; + case PiPWindow.PiPState.STOPPED: + this.player?.updatePlayStatus(true); + this.player?.play(); + this.curState = 'STOPPED'; + this.curError = $r('app.string.current_error_hint'); + break; + case PiPWindow.PiPState.ABOUT_TO_RESTORE: + this.curState = 'ABOUT_TO_RESTORE'; + this.curError = $r('app.string.current_error_hint'); + break; + case PiPWindow.PiPState.ERROR: + this.curState = 'ERROR'; + this.curError = reason; + break; + default: + break; + } } Logger.info(`[${TAG}] onStateChange: ${this.curState}, reason: ${reason}`); } @@ -223,7 +235,9 @@ export struct PlayVideo { if (!this.pipController) { await this.createPipController(); } - this.pipController?.setAutoStartEnabled(this.isAutoPull); + if (canIUse('SystemCapability.Window.SessionManager')) { + this.pipController?.setAutoStartEnabled(this.isAutoPull); + } this.hintMsgVisibility = true; }) } diff --git a/entry/src/main/ets/pages/WindowPip.ets b/entry/src/main/ets/pages/WindowPip.ets index c986194..09b709e 100644 --- a/entry/src/main/ets/pages/WindowPip.ets +++ b/entry/src/main/ets/pages/WindowPip.ets @@ -21,7 +21,7 @@ import Logger from '../utils/Logger'; struct ImageWidthTitle { @State imageHeight: number = 0; @Link pageInfos: NavPathStack; - private titleResource?: Resource; + titleResource?: Resource; build() { Column() { -- Gitee