HarmonyOS 屏幕录制,回到主页,10s内应用会被系统关闭

屏幕录制,home键回到主页后,应用会被关闭,查看日志。

[audio_proc.cpp(CheckAudioCapturer):102] AudioCapturer cannot be used after being frozen. The system will kill the UID: 20020019. sourceType is 2, capturerFlags is 0 

针对这种提情况,尝试方案:

1、无效果:长时间任务,通过。

backgroundTaskManager.startBackgroundRunning(context, ['multiDeviceConnection', 'audioRecording', 'audioPlayback']         , wantAgentObj) 

2、可行:在1的基础上,OH_AVScreenCaptureConfig不设置audioInfo。按照这个设置,那么回到桌面主页,就无法录制音频,请问是否有可以支持回退到主页时,应用可正常录制音频的方案。

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

可以声明KEEP_BACKGROUND_RUNNING权限,允许Service Ability在后台持续运行。

EntryAbility.ets:

import { abilityAccessCtrl, AbilityConstant, common, Permissions, UIAbility, Want,WantAgent,wantAgent } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { backgroundTaskManager } from '@kit.BackgroundTasksKit';

const permissions: Array<Permissions> = ['ohos.permission.MICROPHONE','ohos.permission.READ_MEDIA','ohos.permission.WRITE_MEDIA','ohos.permission.SYSTEM_FLOAT_WINDOW'];
// 使用UIExtensionAbility:将common.UIAbilityContext 替换为common.UIExtensionContext
function reqPermissionsFromUser(permissions: Array<Permissions>, context: common.UIAbilityContext): void {
  let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
  // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
  atManager.requestPermissionsFromUser(context, permissions).then((data) => {
    let grantStatus: Array<number> = data.authResults;
    let length: number = grantStatus.length;
    for (let i = 0; i < length; i++) {
      if (grantStatus[i] === 0) {
        // 用户授权,可以继续访问目标操作
      } else {
        // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
        return;
      }
    }
    // 授权成功
  }).catch((err: BusinessError) => {
    console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
  })
}

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
  }

  onDestroy(): void {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage): void {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
    reqPermissionsFromUser(permissions, this.context);
    windowStage.loadContent('pages/Index', (err) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
    });
  }

  onWindowStageDestroy(): void {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground(): void {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground(): void {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
    let wantAgentInfo: wantAgent.WantAgentInfo = {
      // 点击通知后,将要执行的动作列表
      wants: [
        {
          bundleName: "com.example.myapplication",
          abilityName: "EntryAbility"
        }
      ],
      // 点击通知后,动作类型
      actionType: wantAgent.OperationType.START_ABILITY,
      // 使用者自定义的一个私有值
      requestCode: 0,
      // 点击通知后,动作执行属性
      wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
    };
    try {
      // 通过wantAgent模块下getWantAgent方法获取WantAgent对象
      wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
        try {
          backgroundTaskManager.startBackgroundRunning(this.context,
            backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj).then(() => {
            console.info("Operation startBackgroundRunning succeeded");
          }).catch((error: BusinessError) => {
            console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`);
          });
        } catch (error) {
          console.error(`Operation startBackgroundRunning failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
        }
      });
    } catch (error) {
      console.error(`Operation getWantAgent failed. code is ${(error as BusinessError).code} message is ${(error as BusinessError).message}`);
    }
  }
};
分享
微博
QQ
微信
回复
2天前
相关问题
华为畅享10s能升级鸿蒙系统吗?
10766浏览 • 1回复 待解决
HarmonyOS Navigation主页如何关闭
668浏览 • 1回复 待解决
HarmonyOS 屏幕录制实现
50浏览 • 1回复 待解决
HarmonyOS 录制屏幕 录制摄像头咨询
471浏览 • 1回复 待解决
HarmonyOS 企业内应用下载
115浏览 • 1回复 待解决
HarmonyOS 屏幕录制需要哪些权限?
7浏览 • 1回复 待解决
HarmonyOS 发布企业内应用详细指引
34浏览 • 1回复 待解决
HarmonyOS 系统提供amr播放及录制
374浏览 • 1回复 待解决
荣耀10可以搭载鸿蒙系统吗?
7554浏览 • 1回复 待解决