HarmonyOS 鸿蒙Next屏幕录制的代码示例

发布于 1周前 作者 ionicwang 来自 鸿蒙OS

HarmonyOS 鸿蒙Next屏幕录制的代码示例 请提供屏幕录制的代码示例

6 回复

录制的视频存储在沙盒路径,录制结束保存到了相册:

import { media } from '@kit.MediaKit'
import { BusinessError } from '@kit.BasicServicesKit'
import fs from '@ohos.file.fs';
import { abilityAccessCtrl, common, PermissionRequestResult, Permissions } from '@kit.AbilityKit'
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index13 {

  private avScreenCaptureRecorder: media.AVScreenCaptureRecorder | undefined = undefined;
  private context = this.context as common.UIAbilityContext;
  private filesDir: string = this.context.filesDir; // 保存到沙盒路径 data/storage/el2/base/haps/entry/files
  private filesUri:string = this.filesDir + '/Screen_' + new Date().getTime() + '.mp4';
  private curFile = fs.openSync(this.filesUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  private avCaptureConfig: media.AVScreenCaptureRecordConfig = {
    fd: this.curFile.fd, // 文件需要先有调用者创建,赋予写权限,将文件fd传给此参数
    frameWidth: 640,
    frameHeight: 480
  }

  aboutToAppear(): void {
    let permissions: Array<Permissions> =['ohos.permission.WRITE_IMAGEVIDEO','ohos.permission.READ_IMAGEVIDEO']
    let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
    //requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
    atManager.requestPermissionsFromUser(this.context, permissions).then((data: PermissionRequestResult) => {
      let grantStatus: Array<number> = data.authResults;
      let length: number = grantStatus.length;
      for (let i = 0; i < length; i++) {
        if (grantStatus[i] != 0) {
          //用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
          return;
        }
      }
      console.info(`Success to request permissions from user. authResults is ${grantStatus}.`);
    }).catch((err: BusinessError) => {
      console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
    })
    this.createAVScreenCapture();

  }


  createAVScreenCapture() {
    media.createAVScreenCaptureRecorder().then((captureRecorder: media.AVScreenCaptureRecorder) => {
      if (captureRecorder != null) {
        this.avScreenCaptureRecorder = captureRecorder;
        console.info('Succeeded in createAVScreenCaptureRecorder');
        this.init();
      } else {
        console.error('Failed to createAVScreenCaptureRecorder');
      }
    }).catch((error: BusinessError) => {
      console.error(`createAVScreenCaptureRecorder catchCallback, error message:${error.message}`);
    });
  }

  async init() {
    if (this.avScreenCaptureRecorder !== undefined) {
      await this.avScreenCaptureRecorder.init(this.avCaptureConfig).then(() => {
        console.info('Succeeded in initing avScreenCaptureRecorder');
      }).catch((err: BusinessError) => {
        console.info('Failed to init avScreenCaptureRecorder, error: ' + err.message);
      })
    }
  }


  build() {
    Column({space:30}) {
      Button('开始录屏')
        .onClick(() => {
          this.startRecord()
        })
        .width('100%')
      Button('结束录屏')
        .onClick(() => {
          this.stopRecord()
        })
        .width('100%')
    }
  }

  async startRecord() {
    if (this.avScreenCaptureRecorder !== undefined) {
      await this.avScreenCaptureRecorder.startRecording().then(() => {
        console.info('Succeeded in starting avScreenCaptureRecorder');
      }).catch((err: BusinessError) => {
        console.info('Failed to start avScreenCaptureRecorder, error: ' + err.message);
      })
    }
  }

  async stopRecord() {
    if (this.avScreenCaptureRecorder !== undefined) {
      await this.avScreenCaptureRecorder.stopRecording().then(() => {
        console.info('Succeeded in stopping avScreenCaptureRecorder');
        this.saveVideo();
      }).catch((err: BusinessError) => {
        console.info('Failed to stop avScreenCaptureRecorder, error: ' + err.message);
      })
    }
  }

  saveVideo() {
    let titleStr = 'testVideo' + new Date().getTime()
    let context = getContext(this);
    let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
    console.debug("getPhotoAccessHelper success: ")
    let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.VIDEO;
    let extension:string = 'mp4';
    let options: photoAccessHelper.CreateOptions = {
      title:titleStr
    }
    phAccessHelper.createAsset(photoType, extension, options).then(async (uriDes:string)=>{
      // 使用uri打开文件,可以持续写入内容,写入过程不受时间限制
      try {
        // 写到媒体库文件中
        let file_uri =  fs.openSync(this.filesUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
        let file =  fs.openSync(uriDes, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
        // 内容复制到媒体库文件中
        fs.copyFileSync(file_uri.fd, file.fd);
        fs.closeSync(file.fd);
        fs.closeSync(file_uri.fd);
        promptAction.showToast({
          message: '已保存至相册',
          duration: 2500
        });
      } catch (err) {
        console.error("error is " + JSON.stringify(err))
      }
    }).catch((err:Error)=>{
        console.error("error is " + JSON.stringify(err))
    });
  }
}

注:目前不配置录制宽高的时候,可能会有问题,比如不会出现计时按钮,最终生成的视频也是没有时长。

更多关于HarmonyOS 鸿蒙Next屏幕录制的代码示例的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


这两个权限,现在是受限的,不能申请了啊,怎么解决呢。

不需要申请权限,使用api会自动弹出的那个录制屏幕的权限弹框,文档里面也没写。

但是如果需要录制音频需要麦克风权限。

保存录像文件 需要读和存,放沙盒里面也不需要权限。

那我要保存到图库呢,

在HarmonyOS(鸿蒙)系统中进行屏幕录制的代码示例,通常涉及使用系统提供的API或框架来完成此功能。以下是一个基于ArkUI(鸿蒙的声明式UI框架)的屏幕录制代码示例,使用JavaScript进行编写:

import media from '@ohos.multimedia.media';

function startRecording() {
    media.createScreenRecorder({
        video: {
            width: 1920,
            height: 1080,
            bitrate: 8000000,
            fps: 30
        },
        audio: {
            bitrate: 128000,
            sampleRate: 44100,
            channels: 2
        }
    }).then((recorder) => {
        recorder.start((err) => {
            if (err) {
                console.error('Failed to start recording:', err);
            } else {
                console.log('Recording started successfully');
            }
        });
    }).catch((err) => {
        console.error('Failed to create recorder:', err);
    });
}

// 调用函数开始录制
startRecording();

请注意,上述代码仅为示例,实际开发中可能需要根据具体需求调整参数,并处理更多的错误情况和边缘场景。此外,屏幕录制功能可能受到系统权限和硬件支持的限制,确保应用已获得必要的权限,并在支持的设备上进行测试。

如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html

回到顶部