HarmonyOS 播放视频的时候禁止息屏

HarmonyOS
2024-12-25 11:42:30
1485浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

可以使用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)
      }
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
分享
微博
QQ
微信
回复
2024-12-25 14:25:17


相关问题
鸿蒙开发 打开状态应用怎么防止息?
10559浏览 • 1回复 待解决
HarmonyOS 如何禁止和录
1005浏览 • 1回复 待解决
设置禁止隐私模式
2213浏览 • 1回复 待解决
HarmonyOS 视频播放
578浏览 • 1回复 待解决
HarmonyOS 如何申请禁止权限呢
676浏览 • 1回复 待解决
HarmonyOS如何实现当前禁止
469浏览 • 0回复 待解决
HarmonyOS 如何禁止系统自动息
535浏览 • 1回复 待解决
HarmonyOS如何实现当前禁止
1269浏览 • 1回复 待解决