HarmonyOS OH_AVScreenCapture_StartScreenCapture调用失败

demo:

static napi_value StartScreenCaptureToStream(napi_env env, napi_callback_info info) { 
  OH_AVScreenCaptureConfig config; 
  OH_AudioCaptureInfo micCapInfo = {.audioSampleRate = 48000, .audioChannels = 2, .audioSource = OH_MIC}; 
  OH_VideoCaptureInfo videoCapInfo = { 
    .videoFrameWidth = 720, .videoFrameHeight = 1080, .videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA}; 
  OH_AudioInfo audioInfo = {.micCapInfo = micCapInfo}; 
  OH_VideoInfo videoInfo = {.videoCapInfo = videoCapInfo}; 
  config = { 
    .captureMode = OH_CAPTURE_HOME_SCREEN, 
    .dataType = OH_ORIGINAL_STREAM, 
    .audioInfo = audioInfo, 
    .videoInfo = videoInfo, 
  }; 
  gScreenCapture = OH_AVScreenCapture_Create(); 
  if (nullptr == gScreenCapture) { 
    OH_LOG_INFO(LOG_APP, "[StartScreenCaptureToStream] OH_AVScreenCapture_Create failed!"); 
    return 0; 
  } 
  // 进行初始化操作 
  // 返回 AVSCREEN_CAPTURE_ERR_OK = 0,代表成功;返回 AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT = 2 代表没有权限 
  OH_AVSCREEN_CAPTURE_ErrCode ret = OH_AVScreenCapture_Init(gScreenCapture, config); 
  OH_LOG_INFO(LOG_APP, "[StartScreenCaptureToStream] OH_AVScreenCapture_Init ret: %{public}d", ret); 
  // 设置数据回调 
  ret = OH_AVScreenCapture_SetDataCallback(gScreenCapture, &ScreenCaptureOnBufferAvailable, nullptr); 
  OH_LOG_INFO(LOG_APP, "[StartScreenCaptureToStream] OH_AVScreenCapture_SetDataCallback ret: %{public}d", ret); 
  // 开始录屏 
  ret = OH_AVScreenCapture_StartScreenCapture(gScreenCapture); 
  OH_LOG_INFO(LOG_APP, "[StartScreenCaptureToStream] OH_AVScreenCapture_StartScreenCapture ret: %{public}d", ret); 
  return 0; 
}

调用 OH_AVScreenCapture_StartScreenCapture 会返回 2,AV_SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT。已经添加权限:

ohos.permission.INTERNET 
ohos.permission.READ_MEDIA 
ohos.permission.WRITE_MEDIA 
ohos.permission.MICROPHONE
HarmonyOS
2024-08-14 16:27:46
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

需要通过设置回调OH_AVScreenCapture_SetStateCallback(capture, OnStateChange, userData);来配置权限。 在modules.json5中,三方应用申请不到capture_screen权限。

分享
微博
QQ
微信
回复
2024-08-14 23:28:53
相关问题
HarmonyOS 相机调用失败
83浏览 • 1回复 待解决
HarmonyOS NAPI调用HAR模块失败
377浏览 • 1回复 待解决
HSP调用getRawFileContent失败
669浏览 • 1回复 待解决
startAbility调用失败如何定位
706浏览 • 1回复 待解决
HarmonyOS调用相机设备API获取失败问题
317浏览 • 1回复 待解决
应用调用系统分享框失败
294浏览 • 1回复 待解决
调用SL_IID_BUFFERQUEUE提示失败怎么解决
483浏览 • 1回复 待解决
HarmonyOS 关于OH_AudioRenderer的问题
283浏览 • 1回复 待解决