Ohos

更新时间:2025-07-03 03:44:21

通过阅读本文,您可以了解 鸿蒙端集成SDK的方法。

环境要求

鸿蒙端具体环境要求,更多信息,请参见使用限制

集成SDK

方法一:ohpm 集成(推荐)

  1. 在根目录的oh-package.json5中添加SDK dependencies:

    {
      "name": "demo",
      "version": "1.0.0",
      "description": "Please describe the basic information.",
      "main": "",
      "author": "",
      "license": "",
      "dependencies": {
        "@dingrtc/dingrtc": "3.5.0",
      }
    }
    说明

    此处ohpm 依赖的版本仅供参考,获取最新的ohpm依赖,请参见 SDK下载

  2. app/src/main/module.json5文件中添加如下代码,获取相应的设备权限。

    {
      "module": {
        "name": "Demo",
        "type": "entry",
        "description": "$string:module_desc",
        "mainElement": "DemoAbility",
        "deviceTypes": [
          "phone",
          "tablet",
          "2in1"
        ],
        "deliveryWithInstall": true,
        "installationFree": false,
        "pages": "$profile:main_pages",
        "abilities": [
          {
            "name": "DemoAbility",
            "srcEntry": "./ets/demoability/DemoAbility.ets",
            "description": "$string:DemoAbility_desc",
            "icon": "$media:layered_image",
            "label": "$string:DemoAbility_label",
            "startWindowIcon": "$media:startIcon",
            "startWindowBackground": "$color:start_window_background",
            "exported": true,
            "backgroundModes": [
              "audioRecording",
              "audioPlayback",
              "dataTransfer"
            ],
            "skills": [
              {
                "entities": [
                  "entity.system.home"
                ],
                "actions": [
                  "action.system.home"
                ]
              }
            ]
          }
        ],
        "requestPermissions": [
          {
            "name": "ohos.permission.INTERNET" // 使用网络权限
          },
          {
            "name": "ohos.permission.GET_WIFI_INFO" // 获取WLAN信息权限
          },
          {
            "name": "ohos.permission.GET_NETWORK_INFO" // 获取网络状态权限
          },
          {
            "name": "ohos.permission.KEEP_BACKGROUND_RUNNING" // Demo 长时任务权限,用于后台音频采集和播放
          },
          {
            "name": "ohos.permission.MICROPHONE",
            "reason": "$string:microphone",
            "usedScene": {
              "abilities": [
                "DemoAbility"
              ],
              "when": "inuse"
            }
          },
          {
            "name": "ohos.permission.CAMERA",
            "reason": "$string:camera",
            "usedScene": {
              "abilities": [
                "DemoAbility"
              ],
              "when": "inuse"
            }
          },
        ],
    
      }
    }

    注意:RTC业务一般需要开启长时任务以便于应用退到后台后也可以保持通话

    开启长时服务请参考:上述json 中的backgroundModes 以及 requestPermissions 中关于长时任务的设置。同时在通话页面调用 startContinuousTask 启动后台长时任务(具体可参考github sdk示例代码中https://github.com/aliyun/AliRTCSample/blob/master/Ohos/BasicVideoCall/Demo/src/main/ets/pages/Chat.ets 中关于后台长时任务部分或者参考鸿蒙官网文档: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/continuous-task#%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4)

      startContinuousTask() {
        let wantAgentInfo: wantAgent.WantAgentInfo = {
          // 点击通知后,将要执行的动作列表
          // 添加需要被拉起应用的bundleName和abilityName
          wants: [
            {
              bundleName: "com.ding.rtc.demo",
              abilityName: "DemoAbility"
            }
          ],
          // 指定点击通知栏消息后的动作是拉起ability
          actionType: wantAgent.OperationType.START_ABILITY,
          // 使用者自定义的一个私有值
          requestCode: 0,
          // 点击通知后,动作执行属性
          actionFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
        };
    
        // 通过wantAgent模块下getWantAgent方法获取WantAgent对象
        wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj: WantAgent) => {
          backgroundTaskManager.startBackgroundRunning(this.context,
            backgroundTaskManager.BackgroundMode.AUDIO_RECORDING, wantAgentObj).then(() => {
            // 此处执行具体的长时任务逻辑,如放音等。
            console.info(`Succeeded in operationing startBackgroundRunning.`);
          }).catch((err: BusinessError) => {
            console.error(`Failed to operation startBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
          });
        });
      }
    
      stopContinuousTask() {
        backgroundTaskManager.stopBackgroundRunning(this.context).then(() => {
          console.info(`Succeeded in operationing stopBackgroundRunning.`);
        }).catch((err: BusinessError) => {
          console.error(`Failed to operation stopBackgroundRunning. Code is ${err.code}, message is ${err.message}`);
        });
      }

方法二:手动集成

  1. 下载并解压OHOS SDK,下载地址,请参见SDK下载

  2. 复制SDK文件dingrtc.harApp模块下的libs文件夹中。

    {
      "name": "demo",
      "version": "1.0.0",
      "description": "Please describe the basic information.",
      "main": "",
      "author": "",
      "license": "",
      "dependencies": {
        "@dingrtc/dingrtc": "file:./src/main/libs/dingrtc.har",
      }
    }

后续步骤

完成集成SDK操作后,您可以实现音视频通信的基本功能。具体操作,请参见 Ohos 基本功能

  • 本页导读 (0)
  • 环境要求
  • 集成SDK
  • 后续步骤