HarmonyOS Next开发,Media Kit的AVRecorder适用于什么应用场景开发?

阅读 841
avatarAI BotBETA
HarmonyOS Next开发中,Media Kit的AVRecorder适用于需要音频和视频录制功能的应用场景开发,例如视频会议、在线教育、直播应用、音乐和视频录制工具等。
1 个回答

AVRecorder主要工作是捕获音频信号,接收视频信号,完成音视频编码并保存到文件中,帮助开发者轻松实现音视频录制功能,包括开始录制、暂停录制、恢复录制、停止录制、释放资源等功能控制。它允许调用者指定录制的编码格式、封装格式、文件路径等参数。

示例:

import { media } from '@kit.MediaKit';
import { BusinessError } from '@kit.BasicServicesKit';

const TAG = 'VideoRecorderDemo:';
export class VideoRecorderDemo {
  private avRecorder: media.AVRecorder | undefined = undefined;
  private videoOutSurfaceId: string = "";
  private avProfile: media.AVRecorderProfile = {
    fileFormat : media.ContainerFormatType.CFT_MPEG_4, // 视频文件封装格式,只支持MP4
    videoBitrate : 100000, // 视频比特率
    videoCodec : media.CodecMimeType.VIDEO_AVC, // 视频文件编码格式,支持avc格式
    videoFrameWidth : 640,  // 视频分辨率的宽
    videoFrameHeight : 480, // 视频分辨率的高
    videoFrameRate : 30 // 视频帧率
  };
  private avConfig: media.AVRecorderConfig = {
    videoSourceType : media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV, // 视频源类型,支持YUV和ES两种格式
    profile : this.avProfile,
    url : 'fd://35', //  参考应用文件访问与管理开发示例新建并读写一个文件
    rotation : 0 // 视频旋转角度,默认为0不旋转,支持的值为0、90、180、270
  };

  // 注册avRecorder回调函数
  setAvRecorderCallback() {
    if (this.avRecorder != undefined) {
      // 状态机变化回调函数
      this.avRecorder.on('stateChange', (state: media.AVRecorderState, reason: media.StateChangeReason) => {
        console.info(TAG + 'current state is: ' + state);
      })
      // 错误上报回调函数
      this.avRecorder.on('error', (err: BusinessError) => {
        console.error(TAG + 'error ocConstantSourceNode, error message is ' + err);
      })
    }
  }

  // 相机相关准备工作
  async prepareCamera() {
    // 具体实现查看相机资料
  }

  // 启动相机出流
  async startCameraOutput() {
    // 调用VideoOutput的start接口开始录像输出
  }

  // 停止相机出流
  async stopCameraOutput() {
    // 调用VideoOutput的stop接口停止录像输出
  }

  // 释放相机实例
  async releaseCamera() {
    // 释放相机准备阶段创建出的实例
  }

  // 开始录制对应的流程
  async startRecordingProcess() {
    if (this.avRecorder === undefined) {
      // 1.创建录制实例;
      this.avRecorder = await media.createAVRecorder();
      this.setAvRecorderCallback();
    }
    // 2. 获取录制文件fd;获取到的值传递给avConfig里的url,实现略
    // 3.配置录制参数完成准备工作
    await this.avRecorder.prepare(this.avConfig);
    this.videoOutSurfaceId = await this.avRecorder.getInputSurface();
    // 4.完成相机相关准备工作
    await this.prepareCamera();
    // 5.启动相机出流
    await this.startCameraOutput();
    // 6. 启动录制
    await this.avRecorder.start();

  }

  // 暂停录制对应的流程
  async pauseRecordingProcess() {
    if (this.avRecorder != undefined && this.avRecorder.state === 'started') { // 仅在started状态下调用pause为合理状态切换
      await this.avRecorder.pause();
      await this.stopCameraOutput(); // 停止相机出流
    }
  }

  // 恢复录制对应的流程
  async resumeRecordingProcess() {
    if (this.avRecorder != undefined && this.avRecorder.state === 'paused') { // 仅在paused状态下调用resume为合理状态切换
      await this.startCameraOutput();  // 启动相机出流
      await this.avRecorder.resume();
    }
  }

  async stopRecordingProcess() {
    if (this.avRecorder != undefined) {
      // 1. 停止录制
      if (this.avRecorder.state === 'started'
        || this.avRecorder.state === 'paused' ) { // 仅在started或者paused状态下调用stop为合理状态切换
        await this.avRecorder.stop();
        await this.stopCameraOutput();
      }
      // 2.重置
      await this.avRecorder.reset();
      // 3.释放录制实例
      await this.avRecorder.release();
      // 4.文件录制完成后,关闭fd,实现略
      // 5.释放相机相关实例
      await this.releaseCamera();
    }
  }

  // 一个完整的【开始录制-暂停录制-恢复录制-停止录制】示例
  async videoRecorderDemo() {
    await this.startRecordingProcess();         // 开始录制
    // 用户此处可以自行设置录制时长,例如通过设置休眠阻止代码执行
    await this.pauseRecordingProcess();         //暂停录制
    await this.resumeRecordingProcess();        // 恢复录制
    await this.stopRecordingProcess();          // 停止录制
  }
}

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进