From a1dfa754fe03a568654a56959dd02c214e6b3e62 Mon Sep 17 00:00:00 2001 From: 26thE <962961353@qq.com> Date: Wed, 9 Apr 2025 17:39:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/constants/Constants.ets | 23 ------- .../main/ets/entryability/EntryAbility.ets | 62 ++++++++----------- entry/src/main/ets/pages/AVPlayer.ets | 6 +- entry/src/main/ets/pages/VideoPlay.ets | 62 +++---------------- entry/src/main/ets/pages/WindowPip.ets | 43 +------------ 5 files changed, 39 insertions(+), 157 deletions(-) diff --git a/entry/src/main/ets/constants/Constants.ets b/entry/src/main/ets/constants/Constants.ets index 4606755..2b3a9bb 100644 --- a/entry/src/main/ets/constants/Constants.ets +++ b/entry/src/main/ets/constants/Constants.ets @@ -18,117 +18,94 @@ export class Constants { * NavDestination page name. */ public static readonly NAV_DESTINATION_NAME: string = 'PlayVideo'; - /** * Control width. */ public static readonly CONTROL_WIDTH: string = '95%'; - /** * NavDestination width. */ public static readonly NAV_DESTINATION_WIDTH: string = '100%'; - /** * avDestination height. */ public static readonly NAV_DESTINATION_HEIGHT: string = '100%'; - /** * The hundred percent of the components */ public static readonly FULL_PERCENT: string = '100%'; - /** * XComponent width. */ public static readonly X_COMPONENT_WIDTH: string = '100%'; - /** * Scroll layout width. */ public static readonly SCROLL_LAY_OUT_WEIGHT: number = 1; - /** * Default error information. */ public static readonly ERROR_BY_DEFAULT: string = '无'; - /** * Layout Element Spacing. */ public static readonly SPACE: number = 24; - /** * Error type. */ public static readonly AVPLAYER_TYPE: number = 1; - /** * Idle state. */ public static readonly IDLE_STATUS: string = 'idle'; - /** * Resource Initialization. */ public static readonly INITIALIZED_STATUS: string = 'initialized'; - /** * Prepared state. */ public static readonly PREPARED_STATUS: string = 'prepared'; - /** * Playing status. */ public static readonly PLAYING_STATUS: string = 'playing'; - /** * Paused status. */ public static readonly PAUSED_STATUS: string = 'paused'; - /** * Play to end state. */ public static readonly COMPLETED_STATUS: string = 'completed'; - /** * Stopped status. */ public static readonly STOPPED_STATUS: string = 'stopped'; - /** * Destruction Status. */ public static readonly RELEASED_STATUS: string = 'released'; - /** * Breakpoint sm. */ public static readonly BREAK_POINT_SM: string = 'sm'; - /** * Breakpoint md. */ public static readonly BREAK_POINT_MD: string = 'md'; - /** * Breakpoint lg. */ public static readonly BREAK_POINT_LG: string = 'lg'; - /** * ColumnsTemplate sm. */ public static readonly COLUMNS_TEMPLATE_SM: string = '1fr 1fr'; - /** * ColumnsTemplate md. */ public static readonly COLUMNS_TEMPLATE_MD: string = '1fr 1fr 1fr'; - /** * ColumnsTemplate lg. */ diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 9cd4f5e..9c5a139 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -14,19 +14,18 @@ */ import { Configuration, ConfigurationConstant, UIAbility } from '@kit.AbilityKit'; -import { resourceManager } from '@kit.LocalizationKit'; +import { display, window } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { display, window } from '@kit.ArkUI'; -import Logger from '../utils/Logger'; + +const TAG: string = '[EntryAbility]'; export default class EntryAbility extends UIAbility { - private windowObj?: window.Window; private curBp: string = ''; private updateBreakpoint(windowWidth: number): void { try { - let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels; + let windowWidthVp: number = windowWidth / display.getDefaultDisplaySync().densityPixels; let newBp: string = ''; if (windowWidthVp < 600) { newBp = 'sm'; @@ -39,13 +38,14 @@ export default class EntryAbility extends UIAbility { this.curBp = newBp; AppStorage.setOrCreate('currentBreakpoint', this.curBp); } - } catch (error) { - hilog.error(0x0000, 'EntryAbility', 'updateBreakpoint catch err:', JSON.stringify(error) ?? ''); + } catch (err) { + hilog.error(0x0000, TAG, '%{public}s', + `Update breakpoint failed. code: ${(err as BusinessError).code}, message: ${(err as BusinessError).message}`); } } onCreate(): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onCreate'); AppStorage.setOrCreate('systemColorMode', this.context.config.colorMode); this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); } @@ -60,59 +60,49 @@ export default class EntryAbility extends UIAbility { } onDestroy(): void { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { windowStage.getMainWindow().then((windowObj: window.Window) => { - let type = window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR; - let avoidArea = windowObj.getWindowAvoidArea(type); - let bottomRectHeight = avoidArea.bottomRect.height; - AppStorage.setOrCreate('bottomRectHeight', bottomRectHeight); - type = window.AvoidAreaType.TYPE_SYSTEM; - avoidArea = windowObj.getWindowAvoidArea(type); - let topRectHeight = avoidArea.topRect.height; - AppStorage.setOrCreate('topRectHeight', topRectHeight); - this.windowObj = windowObj; - this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width); windowObj.on('windowSizeChange', (windowSize) => { this.updateBreakpoint(windowSize.width); }) + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, '%{public}s', + `Failed to get main window. code: ${error.code}, message: ${error.message}`); }); // Main window is created, set main page for this ability - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - let context = this.context; - let resourceManager: resourceManager.ResourceManager = context.resourceManager; - AppStorage.setOrCreate('resourceManager', resourceManager); - windowStage.loadContent('pages/WindowPip', (err, data) => { - if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageCreate'); + windowStage.loadContent('pages/WindowPip', (error: BusinessError) => { + if (error.code) { + hilog.error(0x0000, TAG, '%{public}s', + `Failed to load the content. code: ${error.code}, message: ${error.message}`); return; } - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); + hilog.info(0x0000, TAG, '%{public}s', 'Succeeded in loading the content.'); let windowClass: window.Window = windowStage.getMainWindowSync(); - AppStorage.setOrCreate('windowClass', windowClass); - let isLayoutFullScreen = true; - windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => { - Logger.info('Succeeded in setting the window layout to full-screen mode.'); - }).catch((err: BusinessError) => { - Logger.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); + windowClass.setWindowLayoutFullScreen(true).then(() => { + hilog.info(0x0000, TAG, `%{public}s`, 'Succeeded in setting the window layout to full-screen mode.'); + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, '%{public}s', + `UFailed to set the window layout to full-screen mode. code: ${error.code}, message: ${error.message}`); }); }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); } } \ No newline at end of file diff --git a/entry/src/main/ets/pages/AVPlayer.ets b/entry/src/main/ets/pages/AVPlayer.ets index 60181ef..f9b5374 100644 --- a/entry/src/main/ets/pages/AVPlayer.ets +++ b/entry/src/main/ets/pages/AVPlayer.ets @@ -13,9 +13,9 @@ * limitations under the License. */ -import { resourceManager } from '@kit.LocalizationKit'; -import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit'; +import { BusinessError } from '@kit.BasicServicesKit'; +import { resourceManager } from '@kit.LocalizationKit'; import { media } from '@kit.MediaKit'; import { Constants } from '../constants/Constants'; import Logger from '../utils/Logger'; @@ -75,7 +75,7 @@ export class AVPlayer { Logger.info('AVPlayer state idle called.'); if (!this.jumpNext) { // Call the release operation to destroy the instance object. - this.avPlayer.release(); + this.avPlayer?.release(); } else { let context = getContext(this) as common.UIAbilityContext; let fileDescriptor: resourceManager.RawFileDescriptor; diff --git a/entry/src/main/ets/pages/VideoPlay.ets b/entry/src/main/ets/pages/VideoPlay.ets index 51c6484..c587f60 100644 --- a/entry/src/main/ets/pages/VideoPlay.ets +++ b/entry/src/main/ets/pages/VideoPlay.ets @@ -13,9 +13,7 @@ * limitations under the License. */ -import { ConfigurationConstant } from '@kit.AbilityKit'; -import { BusinessError } from '@kit.BasicServicesKit'; -import { PiPWindow, window } from '@kit.ArkUI'; +import { PiPWindow } from '@kit.ArkUI'; import { Constants } from '../constants/Constants'; import { AVPlayer } from './AVPlayer'; import Logger from '../utils/Logger'; @@ -63,19 +61,12 @@ function dataListColumn() { @Component export struct PlayVideo { - @StorageProp('systemColorMode') @Watch('onColorModeChange') - currentMode: ConfigurationConstant.ColorMode = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT; - @StorageLink('topRectHeight') topRectHeight: number = 0; - @StorageLink('bottomRectHeight') bottomRectHeight: number = 0; @StorageLink('currentBreakpoint') curBp: string = Constants.BREAK_POINT_SM; - @Consume('pageInfos') pageInfos: NavPathStack; @State curState: string = ''; @State curError: ResourceStr = ''; @State buttonAction: string = ''; @State isAutoPull: boolean = false; - @State isLightBackground: boolean = false; @State hintMsgVisibility: boolean = false; - @State pipTypeString: string = ''; private mXComponentController = new XComponentController(); private surfaceId = ''; private navigationId: string = ''; @@ -85,8 +76,6 @@ export struct PlayVideo { private scrollerForScroll: Scroller = new Scroller(); private defaultListData: Array = [1, 2, 3, 4, 5, 6]; private pathStack: NavPathStack = new NavPathStack(); - // How to get the main window. - private mainWin: window.Window = AppStorage.get('windowClass') as window.Window; aboutToAppear(): void { this.eventHub.on('onStateChange', (fg: boolean) => { @@ -94,52 +83,17 @@ export struct PlayVideo { this.stopPip(); } }); - this.onColorModeChange(); } aboutToDisappear(): void { - this.backColorModeChange(); - this.pipController?.off('stateChange'); - } - - backColorModeChange(): void { - let fontStatusColor = ''; - if (this.currentMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) { - // Currently in light mode, resource initialization logic. - fontStatusColor = '#000000'; - } else { - // The current mode is dark, resource initialization logic. - fontStatusColor = '#FFFFFF'; - } - this.setStatusStyle(fontStatusColor) - } - - onColorModeChange(): void { - let fontStatusColor = ''; - if (this.currentMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) { - // Currently in light mode, resource initialization logic. - fontStatusColor = '#FFFFFF'; - } else { - // The current mode is dark, resource initialization logic. - fontStatusColor = '#FFFFFF'; - } - this.setStatusStyle(fontStatusColor) - } - - setStatusStyle(statusBarContentColor: string): void { - let systemBarProperties: window.SystemBarProperties = { - statusBarContentColor: statusBarContentColor - }; - try { - let promise = this.mainWin.setWindowSystemBarProperties(systemBarProperties); - promise.then(() => { - Logger.info('Succeeded in setting the system bar properties.'); - }).catch((err: BusinessError) => { - Logger.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`); - }); - } catch (exception) { - Logger.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`); + this.destroyPipController(); + if (this.player) { + this.player.stopAvPlayer(); + this.player = undefined; } + this.eventHub?.off('onStateChange'); + this.pipController?.off('stateChange'); + this.pipController?.off('controlPanelActionEvent'); } async startPip(): Promise { diff --git a/entry/src/main/ets/pages/WindowPip.ets b/entry/src/main/ets/pages/WindowPip.ets index 8418425..c986194 100644 --- a/entry/src/main/ets/pages/WindowPip.ets +++ b/entry/src/main/ets/pages/WindowPip.ets @@ -13,9 +13,6 @@ * limitations under the License. */ -import { ConfigurationConstant } from '@kit.AbilityKit'; -import { BusinessError } from '@kit.BasicServicesKit'; -import { window } from '@kit.ArkUI'; import { Constants } from '../constants/Constants'; import { PlayVideo } from './VideoPlay'; import Logger from '../utils/Logger'; @@ -50,7 +47,7 @@ struct ImageWidthTitle { this.pageInfos?.pushPath({ name: Constants.NAV_DESTINATION_NAME }, { launchMode: 3 }); }) .onAreaChange((oldArea: Area, newArea: Area) => { - let width = newArea.width as number; + let width: number = newArea.width as number; this.imageHeight = width * 9 / 16; Logger.info(`[onAreaChange] oldArea:${oldArea} ,newArea:${newArea}`); }) @@ -60,8 +57,6 @@ struct ImageWidthTitle { @Entry @Component struct WindowPip { - @StorageProp('systemColorMode') @Watch('onColorModeChange') - currentMode: ConfigurationConstant.ColorMode = ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT; @StorageLink('topRectHeight') topRectHeight: number = 0; @StorageLink('bottomRectHeight') bottomRectHeight: number = 0; @StorageLink('currentBreakpoint') curBp: string = Constants.BREAK_POINT_SM; @@ -75,40 +70,6 @@ struct WindowPip { $r('app.string.video10'), $r('app.string.video11'), $r('app.string.video12'), $r('app.string.video13'), $r('app.string.video14'), $r('app.string.video15') ]; - // How to get the main window. - private mainWin: window.Window = AppStorage.get('windowClass') as window.Window; - - aboutToAppear(): void { - this.onColorModeChange(); - } - - onColorModeChange(): void { - let fontStatusColor = ''; - if (this.currentMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) { - // Currently in light mode, resource initialization logic. - fontStatusColor = '#000000'; - } else { - // The current mode is dark, resource initialization logic. - fontStatusColor = '#FFFFFF'; - } - this.setStatusStyle(fontStatusColor) - } - - setStatusStyle(statusBarContentColor: string): void { - let systemBarProperties: window.SystemBarProperties = { - statusBarContentColor: statusBarContentColor - }; - try { - let promise = this.mainWin.setWindowSystemBarProperties(systemBarProperties); - promise.then(() => { - Logger.info('Succeeded in setting the system bar properties.'); - }).catch((err: BusinessError) => { - Logger.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`); - }); - } catch (exception) { - Logger.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`); - } - } @Builder PageMap(name: string) { @@ -169,4 +130,4 @@ struct WindowPip { .size({ width: Constants.NAV_DESTINATION_WIDTH, height: Constants.NAV_DESTINATION_HEIGHT }) .id(this.navigationId) } -} +} \ No newline at end of file -- Gitee