From 7a051802d373951a3aa9cb796f5352609098ed54 Mon Sep 17 00:00:00 2001 From: xieyifeng Date: Tue, 2 Sep 2025 19:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=92=AD=E6=94=BE=E5=BC=80?= =?UTF-8?q?=E5=8F=91=E5=AE=9E=E8=B7=B5=E4=BB=A3=E7=A0=81AI=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/controller/AvPlayerController.ets | 10 +++++++++ .../ets/controller/AvSessionController.ets | 2 +- .../ets/controller/AvSessionController1.ets | 2 +- .../entry/src/main/ets/view/AVPlayer.ets | 21 +++++++++++-------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets index 3e0727e2..38fc91a3 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/controller/AvPlayerController.ets @@ -138,12 +138,22 @@ export class AvPlayerController { case audio.InterruptHint.INTERRUPT_HINT_PAUSE: // This branch indicates that the system has paused the audio stream (temporarily lost focus). In order to keep the state consistent, the application needs to switch to the audio paused state. // Temporary loss of focus: after other audio streams release the audio focus, this audio stream will receive the audio interruption event corresponding to resume, and then it can continue playing by itself. + /** + * Update play status + * @param isPlay is it playing + * @returns void + */ this.updateIsPlay(false); this.pauseVideo(); break; case audio.InterruptHint.INTERRUPT_HINT_STOP: // This branch indicates that the system has stopped the audio stream (permanently lost focus). In order to keep the state consistent, the application needs to switch to the audio pause state. // Permanent loss of focus: no audio interruption events will be received in the future. If you want to resume playing, you need the user to trigger it actively. + /** + * Update play status + * @param isPlay is it playing + * @returns void + */ this.updateIsPlay(false); this.pauseVideo(); break; diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets index 587fc038..54f6bfe1 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController.ets @@ -26,7 +26,7 @@ const TAG = 'AvSessionController'; export class AvSessionController { private static instance: AvSessionController | null; private context: common.UIAbilityContext | undefined = undefined; - private avSession: avSession.AVSession | undefined = undefined; + private avSession?: avSession.AVSession; private avSessionMetadata: avSession.AVMetadata | undefined = undefined; constructor() { diff --git a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController1.ets b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController1.ets index 172eaabd..940dc40a 100644 --- a/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController1.ets +++ b/VideoPlayerSample/MediaService/src/main/ets/controller/AvSessionController1.ets @@ -20,7 +20,7 @@ import { avSession } from '@kit.AVSessionKit'; const TAG = 'AvSessionController'; export class AvSessionController { - private avSession: avSession.AVSession | undefined = undefined; + private avSession?: avSession.AVSession; private context: common.UIAbilityContext | undefined = undefined; public initAvSession() { this.context = AppStorage.get('context'); diff --git a/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets b/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets index 79faaa81..67c177f8 100644 --- a/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets +++ b/VideoPlayerSample/entry/src/main/ets/view/AVPlayer.ets @@ -318,8 +318,7 @@ export struct VideoPlayer { // On the right side of the screen relative to the X-axis coordinate in the upper left corner of the application window, adjust the brightness. this.visible = true; let curBrightness = this.screenBrightness - this.getUIContext().vp2px(event.offsetY) / this.getUIContext().vp2px(this.screenHeight); - curBrightness = curBrightness >= 1.0 ? 1.0 : curBrightness; - curBrightness = curBrightness <= 0.0 ? 0.0 : curBrightness; + curBrightness = Math.max(0, Math.min(1.0, curBrightness)); this.screenBrightness = curBrightness; hilog.info(0x0000, 'AVPlayer', `this brightness is: ` + this.screenBrightness); @@ -560,7 +559,7 @@ export struct VideoPlayer { } // 前台进度条变化接口 - // [Start slider] + // [Start slider1] sliderOnchange(seconds: number, mode: SliderChangeMode) { let seekTime: number = seconds * this.avPlayerController.duration / this.avPlayerController.durationTime; this.currentStringTime = secondToTime(Math.floor(seekTime / CommonConstants.SECOND_TO_MS)); @@ -571,27 +570,31 @@ export struct VideoPlayer { case SliderChangeMode.Click: break; case SliderChangeMode.Moving: - // [StartExclude slider] + // [StartExclude slider1] this.isSliderDragging = true; this.isTimeDisplay = 1; this.trackThicknessSize = CommonConstants.TRACK_SIZE_MAX; this.sliderStyle = SliderStyle.OutSet; - // [EndExclude slider] + // [EndExclude slider1] break; case SliderChangeMode.End: - this.avPlayerController.seek(seekTime); // Call the seek method of AVPlayer to control the playback progress. - // [StartExclude slider] + try { + this.avPlayerController.seek(seekTime); // Call the seek method of AVPlayer to control the playback progress. + } catch (err) { + Logger.error(TAG, `AVPlsyer seek failed, err.code:${err.code}, err.message:${err.message}`); + } + // [StartExclude slider1] this.isTimeDisplay = 0; this.trackThicknessSize = CommonConstants.TRACK_SIZE_MIN; this.sliderStyle = SliderStyle.NONE; this.isSliderDragging = false; - // [EndExclude slider] + // [EndExclude slider1] break; default: break; } } - // [End slider] + // [End slider1] async iconOnclick() { if (this.avPlayerController.isPlaying) { -- Gitee