From d0362806163f9fbb23ba8180bf0304151966ef1e Mon Sep 17 00:00:00 2001 From: hdw Date: Fri, 23 May 2025 16:56:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E6=94=B9=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/controller/AvPlayerController.ets | 8 +++++--- .../src/main/ets/controller/AvSessionController.ets | 4 ++-- .../MediaService/src/main/ets/utils/ImageUtil.ets | 6 ++---- .../entry/src/main/ets/entryability/EntryAbility.ets | 7 ++++--- .../entry/src/main/ets/pages/IndexPageDocs.ets | 4 +--- VideoPlayerSample/entry/src/main/ets/utils/WindowUtil.ets | 5 +++-- VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets | 8 +++----- 7 files changed, 20 insertions(+), 22 deletions(-) diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets index 351eb047..c533cacf 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets @@ -43,10 +43,12 @@ export class AvPlayerController { private curSource: VideoData; private context: common.UIAbilityContext | undefined = AppStorage.get('context'); private avSessionController: AvSessionController; + private uiContext: UIContext - constructor(curSource: VideoData) { + constructor(curSource: VideoData, context: UIContext) { this.curSource = curSource; this.avSessionController = AvSessionController.getInstance(); + this.uiContext = context; } public initAVPlayer() { @@ -65,7 +67,7 @@ export class AvPlayerController { } this.setAVPlayerCallback(this.avPlayer); if (this.curIndex === this.curSource.index) { - this.avSessionController.setAVMetadata(this.curSource, 0); + this.avSessionController.setAVMetadata(this.curSource, 0, this.uiContext); } this.setAvSessionListener(); Logger.info(TAG, 'createAVPlayer success:' + ` this.curIndex:${this.curIndex}`); @@ -331,7 +333,7 @@ export class AvPlayerController { this.avPlayer.state !== AVPlayerState.COMPLETED) { return; } - this.avSessionController.setAVMetadata(this.curSource, this.duration); + this.avSessionController.setAVMetadata(this.curSource, this.duration, this.uiContext); //Writing in the avPlayer.play callback can cause a delay in the state change of the playback control center. this.updateIsPlay(true); Logger.info(TAG, `playVideo: state:${this.avPlayer.state} this.curIndex:${this.curIndex}`); diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets index caf81094..31df8d0d 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets @@ -70,14 +70,14 @@ export class AvSessionController { } // [Start meta_data] - public async setAVMetadata(curSource: VideoData, duration: number) { + public async setAVMetadata(curSource: VideoData, duration: number, uiContext: UIContext) { // [StartExclude meta_data] if (curSource === undefined) { Logger.error(TAG, 'SetAVMetadata Error, curSource is null'); return; } // [EndExclude meta_data] - const imagePixMap = await ImageUtil.getPixmapFromMedia(curSource.head); + const imagePixMap = await ImageUtil.getPixmapFromMedia(curSource.head, uiContext); let metadata: avSession.AVMetadata = { assetId: `${curSource.index}`, // Media ID title: this.context?.resourceManager.getStringSync(curSource.name), // title diff --git a/VideoPlayerSample/MediaService/src/main/ets/utils/ImageUtil.ets b/VideoPlayerSample/MediaService/src/main/ets/utils/ImageUtil.ets index c171f98a..dc9b6f8b 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/utils/ImageUtil.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/utils/ImageUtil.ets @@ -14,11 +14,9 @@ */ import { image } from '@kit.ImageKit'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); -let context = uiContext!.getHostContext()!; export class ImageUtil { - public static async getPixmapFromMedia(resource: Resource) { - let unit8Array = await context.resourceManager?.getMediaContent({ + public static async getPixmapFromMedia(resource: Resource, uiContext: UIContext) { + let unit8Array = await uiContext.getHostContext()!.resourceManager?.getMediaContent({ bundleName: resource.bundleName, moduleName: resource.moduleName, id: resource.id diff --git a/VideoPlayerSample/entry/src/main/ets/entryability/EntryAbility.ets b/VideoPlayerSample/entry/src/main/ets/entryability/EntryAbility.ets index 2eed948f..09e0d336 100644 --- a/VideoPlayerSample/entry/src/main/ets/entryability/EntryAbility.ets +++ b/VideoPlayerSample/entry/src/main/ets/entryability/EntryAbility.ets @@ -34,8 +34,8 @@ export default class EntryAbility extends UIAbility { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); let windowClass: window.Window = windowStage.getMainWindowSync(); + AppStorage.setOrCreate('windowStage', windowStage); // [EndExclude stage_creat] - WindowUtil.getInstance().setWindowStage(windowStage); // [StartExclude stage_creat] windowClass.setWindowLayoutFullScreen(true); windowClass.setWindowSystemBarProperties({ @@ -47,8 +47,9 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); - let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext() - AppStorage.setOrCreate('uiContext', uiContext); + // [EndExclude stage_creat] + WindowUtil.getInstance().setWindowStage(windowStage); + // [StartExclude stage_creat] }); // [EndExclude stage_creat] } diff --git a/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets b/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets index a4b22f12..4cefd9f1 100644 --- a/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets +++ b/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets @@ -21,8 +21,6 @@ import { RightSide, VideoDes } from '../view/VideoSide'; import { VideoList } from '../view/VideoList'; import { VideoPlayer } from '../view/AVPlayer'; import { VideoDetails } from '../view/VideoDetails'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); -let context1 = uiContext!.getHostContext()!; const TAG = '[IndexPage]'; // [Start index_page1] @@ -43,7 +41,7 @@ struct IndexPage { // 在aboutToAppear中开启窗口尺寸监听 async aboutToAppear(): Promise { - let context = context1; + let context = this.getUIContext().getHostContext()!; let windowClass = await window.getLastWindow(context); await windowClass.setWindowKeepScreenOn(true); //注册窗口尺寸监听 diff --git a/VideoPlayerSample/entry/src/main/ets/utils/WindowUtil.ets b/VideoPlayerSample/entry/src/main/ets/utils/WindowUtil.ets index bd9e4ca0..ba8bfb46 100644 --- a/VideoPlayerSample/entry/src/main/ets/utils/WindowUtil.ets +++ b/VideoPlayerSample/entry/src/main/ets/utils/WindowUtil.ets @@ -34,6 +34,7 @@ export class WindowUtil { // [Start set_stage1] public setWindowStage(windowStage: window.WindowStage): void { + let uiContext = windowStage.getMainWindowSync().getUIContext(); this.windowStage = windowStage; this.windowStage.getMainWindow((err, windowClass: window.Window) => { // [StartExclude set_stage1] @@ -53,8 +54,8 @@ export class WindowUtil { AppStorage.setOrCreate('deviceWidth', properties.windowRect.width); // 设置窗口宽度 AppStorage.setOrCreate('deviceHeight', properties.windowRect.height); // 设置窗口高度 // [StartExclude set_stage1] - AppStorage.setOrCreate('statusBarHeight', uiContext!.px2vp(area.topRect.height)); - AppStorage.setOrCreate('navBarHeight', uiContext!.px2vp(naviBarArea.bottomRect.height)); + AppStorage.setOrCreate('statusBarHeight', uiContext.px2vp(area.topRect.height)); + AppStorage.setOrCreate('navBarHeight', uiContext.px2vp(naviBarArea.bottomRect.height)); // [EndExclude set_stage1] }); } diff --git a/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets b/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets index 4b8f0dab..6ce19417 100644 --- a/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets +++ b/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets @@ -20,8 +20,6 @@ import { audio, AVVolumePanel } from '@kit.AudioKit'; import { Logger, CommonConstants, secondToTime, } from '@ohos/MediaService'; import { AvPlayerController, VideoData } from '@ohos/MediaService'; import { FullScreenControl, VideoBottom } from './VideoSide'; -const uiContext: UIContext | undefined = AppStorage.get('uiContext'); -let context1 = uiContext!.getHostContext()!; const TAG = '[VideoPlayer]'; @Preview @@ -39,7 +37,7 @@ export struct VideoPlayer { @Prop @Watch('onIndexChange') curIndex: number = CommonConstants.CURINDEX_DEFAULT_NUM; @State isTimeDisplay: number = 0; @State trackThicknessSize: number = CommonConstants.TRACK_SIZE_MIN; - @State avPlayerController: AvPlayerController = new AvPlayerController(this.curSource); + @State avPlayerController: AvPlayerController = new AvPlayerController(this.curSource, this.getUIContext()); @State sliderStyle: SliderStyle = SliderStyle.NONE; @State isShowTips: boolean = false; @State isSliderDragging: boolean = false; @@ -70,7 +68,7 @@ export struct VideoPlayer { aboutToAppear(): void { let windowClass: window.Window | undefined = undefined; - const context: Context = context1; + const context: Context = this.getUIContext().getHostContext()!; settings.getValue(context, settings.display.SCREEN_BRIGHTNESS_STATUS, settings.domainName.DEVICE_SHARED) .then((value) => { hilog.info(0x0000, 'AVPlayer', `Promise:value -> ${JSON.stringify(value)}`); @@ -78,7 +76,7 @@ export struct VideoPlayer { }) try { - window.getLastWindow(context1, (err, data) => { + window.getLastWindow(this.getUIContext().getHostContext()!, (err, data) => { if(err) { hilog.error(0x0000, 'AVPlayer', `Failed to obtain the top window. Cause code: ${err.code}, message: ${err.message}`); -- Gitee From 69b45809e665dfb5c7300ad53822f43f6ab6ead1 Mon Sep 17 00:00:00 2001 From: hdw Date: Fri, 23 May 2025 17:02:08 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E6=8A=A5?= =?UTF-8?q?=E7=BA=A2=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets | 2 ++ 1 file changed, 2 insertions(+) diff --git a/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets b/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets index 4cefd9f1..66aefbca 100644 --- a/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets +++ b/VideoPlayerSample/entry/src/main/ets/pages/IndexPageDocs.ets @@ -208,5 +208,7 @@ struct IndexPage { }); } // [EndExclude index_page1] + onWindowSizeChange(changedPropertyName: string) { + } } // [End index_page1] \ No newline at end of file -- Gitee From d3fb43b2e94df7747f47079cde212c7d8eea040d Mon Sep 17 00:00:00 2001 From: hdw Date: Sat, 24 May 2025 19:34:38 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix=EF=BC=9A=E5=88=A0=E9=99=A4=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=A0=87=E8=AF=86=E5=A4=9A=E4=BD=99=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AvSessionControlleDocsr.ets | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionControlleDocsr.ets diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionControlleDocsr.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionControlleDocsr.ets deleted file mode 100644 index 891abaa8..00000000 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionControlleDocsr.ets +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -// [Start av_session] -import { avSession } from '@kit.AVSessionKit'; - -export class AvSessionController { - private avSession: avSession.AVSession | undefined = undefined; - - public initAvSession() { - this.context = AppStorage.get('context'); - if (!this.context) { - hilog.info(0x0001, TAG, "session create failed : context is undefined"); - return; - } - // Create an AVSession with the session type set to' video'. - avSession.createAVSession(this.context, "SHORT_AUDIO_SESSION", 'video').then(async (avSession) => { - this.avSession = avSession; - hilog.info(0x0001, TAG, `session create successed : sessionId : ${this.avSession.sessionId}`); - // Sets the UIAbility for being pulled up by the broadcast control center. - this.setLaunchAbility(); - // Activate media session - this.avSession.activate(); - }); - } -} -// [End av_session] \ No newline at end of file -- Gitee