可以使用setWindowKeepScreenOn实现,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#windowgetlastwindow9不休眠的视频例子:import { BusinessError } from '@kit.BasicServicesKit'; import { window } from '@kit.ArkUI'; @Entry @Component struct VideoPage4 { @State message: string = 'Hello World'; @State videoSrc: Resource = $rawfile('video.mp4') @State previewUri: Resource = $r('app.media.startIcon') @State curRate: PlaybackSpeed = PlaybackSpeed.Speed_Forward_1_00_X @State isAutoPlay: boolean = false @State showControls: boolean = true controller: VideoController = new VideoController() @State isKeepScreenOn: boolean = true; setWindowKeep() { try { let windowClass: window.Window | undefined = undefined; let promise = window.getLastWindow(getContext()); promise.then((data) => { windowClass = data; windowClass.setWindowKeepScreenOn(this.isKeepScreenOn, (err: BusinessError) => { const errCode: number = err.code; if (errCode) { console.error(`Failed to set the screen to be always on. Cause code: ${err.code}, message: ${err.message}`); return; } console.info('Succeeded in setting the screen to be always on.'); }); }) } catch (exception) { console.error(`Failed to set the screen to be always on. Cause code: ${exception.code}, message: ${exception.message}`); } } build() { Column(){ Video({ src: this.videoSrc, previewUri: this.previewUri, currentProgressRate: this.curRate, controller: this.controller }) .width('100%') .height(600) .autoPlay(this.isAutoPlay) .controls(this.showControls) .onStart(() => { console.info('onStart') }) .onPause(() => { console.info('onPause') }) .onFinish(() => { console.info('onFinish') }) .onError(() => { console.info('onError') }) .onStop(() => { console.info('onStop') }) Row() { Button('start').onClick(() => { this.controller.start() // 开始播放 this.setWindowKeep() }).margin(2) Button('pause').onClick(() => { this.controller.pause() // 暂停播放 }).margin(2) Button('stop').onClick(() => { this.controller.stop() // 结束播放 }).margin(2) Button('reset').onClick(() => { this.controller.reset() // 重置AVPlayer }).margin(2) Button('setTime').onClick(() => { this.controller.setCurrentTime(10, SeekMode.Accurate) // 精准跳转到视频的10s位置 }).margin(2) } } } }
可以使用setWindowKeepScreenOn实现,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#windowgetlastwindow9
不休眠的视频例子: