1 个回答

1.需要声明两个权限[‘ohos.permission.CAMERA’,‘ohos.permission.MICROPHONE’]

2.使用XComponent组件配合官网示例即可运行

@Component
struct XComponentPage {
  // 创建XComponentController
  mXComponentController: XComponentController = new XComponentController;
  surfaceId: string = '';

  build() {
    Flex({ wrap: FlexWrap.Wrap }) {
      // 创建XComponent
      XComponent({
        id: '',
        type: 'surface',
        libraryname: '',
        controller: this.mXComponentController
      })
        .onLoad(() => {
          console.log(`loading start`);
          // 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置
          // 预览流与录像输出流的分辨率的宽高比要保持一致1344x2772
          this.mXComponentController.setXComponentSurfaceSize({ surfaceWidth: 640, surfaceHeight: 480 });
          // 获取Surface ID
          this.surfaceId = this.mXComponentController.getXComponentSurfaceId();
          console.log(`loading end surfaceId = ${JSON.stringify(this.surfaceId)}`);
        })
        .width(640)
        .height(480)
      SaveButton()
        .onClick(async () => {
          let permissions: Permissions[] = ['ohos.permission.CAMERA','ohos.permission.MICROPHONE']
          let atManager = abilityAccessCtrl.createAtManager();
          await atManager.requestPermissionsFromUser(getContext(), permissions);
          videoRecording(getContext(), this.surfaceId)//官网示例函数
        })
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进