开发者在成功接入Talk SDK简介后,无需部署任何服务器即可拥有实时语音讲话能力。
库
@youme/voice_engine
包含了必须的接口和so库,请导入HarmonyOS工程。
YouMeRtcAPI
引擎服务类,单例模式。该类提供SDK操作的相关方法,例如:初始化,加入房间,参数设置等。
YouMeRtcUserRole
用户通话角色。
YouMeRtcEvent
SDK事件类型
YouMeRtcErrorCode
错误码
YouMeRtcServerRegion
SDK语音服务地域。
两种方法导入HAR包:
下载youme_voice_engine_3.0.15.har包,放到⼯程libs中。在⼯程中配置引⽤:
"dependencies": {
"@youme/voice_engine":"file:./libs/youme_voice_engine_3.0.15.har",
}
在⼯程中配置:
"dependencies": {
"@youme/voice_engine":"3.0.15",
}
运⾏命令:
ohpm install @youme/voice_engine
在模块的 module.json5 中 requestPermissions添加网络和麦克风权限,配置如下:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET",
},
{
"name": "ohos.permission.MICROPHONE",
"reason": "$string:mic_reason",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "inuse"
}
}
1) SDK调用示例 - 设置SDK事件回调和成员列表变更回调:
import { YouMeRtcAPI } from '@youme/voice_engine';
let curDate = new Date();
YouMeRtcAPI.Instance().SetEventCallback(
(event, errCode, channel, param) => {
const ctx = getContext(this)
ctx.eventHub.emit("youme_event", event, errCode, channel, param)
}
);
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【api】设置事件回调\n`
YouMeRtcAPI.Instance().SetMemberChangeCallback(
(channel, members, isUpdate) => {
const ctx = getContext(this)
ctx.eventHub.emit("youme_members", channel, members, isUpdate)
}
);
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【api】设置成员列表回调\n`
SDK调用示例 - 初始化SDK:
import { YouMeRtcAPI } from '@youme/voice_engine';
const promise = new Promise<number>((resolve, reject) => {
let error = YouMeVoiceAPI.Instance().Init(this.appCustom.appKey, this.appCustom.appSecret, this.selectedRegion, "");
if (error == 0) {
resolve(error);
} else {
reject(new Error("error call failed"));
}
});
promise
.then(result => {
hilog.info(logFlag, logTag, `call init sdk succ:${result} region:${this.selectedRegion}`);
})
.catch((e: Error) => {
hilog.error(logFlag, logTag, `call init sdk err:${e.message} zone:${this.selectedRegion}`);
});
SDK调用示例 - 监听SDK事件:
import { YouMeRtcErrorCode, YouMeRtcEvent } from '@youme/voice_engine';
eventLister: Function = (event:YouMeRtcEvent, err:YouMeRtcErrorCode, channel:string, param:string) => {
let curDate = new Date();
switch (event){
case YouMeRtcEvent.YOUME_EVENT_LOCAL_MIC_ON:
case YouMeRtcEvent.YOUME_EVENT_LOCAL_MIC_OFF:
case YouMeRtcEvent.YOUME_EVENT_LOCAL_SPEAKER_ON:
case YouMeRtcEvent.YOUME_EVENT_LOCAL_SPEAKER_OFF:
case YouMeRtcEvent.YOUME_EVENT_SWITCH_OUTPUT:{
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【event】${eventStrings[event]} 错误码:${err} 频道:${channel} 参数:${param}\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_MY_MIC_LEVEL:{
this.selfVolumeLevel = err
break;
}
case YouMeRtcEvent.YOUME_EVENT_FAREND_VOICE_LEVEL:{
this.farendVolumeLevel = err
this.farendUser = param
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_VOICE_ON:{
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【vad】${eventStrings[event]} 用户${param}开始说话\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_VOICE_OFF:{
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【vad】${eventStrings[event]} 用户${param}停止说话\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_PAUSED: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【vad】${eventStrings[event]} 通话暂停${err}\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_RESUMED: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【vad】${eventStrings[event]} 通话恢复${err}\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_MIC_ON: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【event】${eventStrings[event]} 用户${param}麦克风开启\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_MIC_OFF: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【event】${eventStrings[event]} 用户${param}麦克风关闭\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_SPEAKER_ON: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【event】${eventStrings[event]} 用户${param}扬声器开启\n`
break;
}
case YouMeRtcEvent.YOUME_EVENT_OTHERS_SPEAKER_OFF: {
this.sdkEventInfo += `${curDate.toLocaleTimeString()} 【event】${eventStrings[event]} 用户${param}扬声器关闭\n`
break;
}
default:{
break;
}
}
}
调用SDK C++ API,需要先在Native C++代码的CMakeList.txt中配置依赖的SDK头文件和库文件路径,示例如下:
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(entry)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
if(DEFINED PACKAGE_FIND_FILE)
include(${PACKAGE_FIND_FILE})
endif()
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include
${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules/@youme/voice_engine/include)
set(YOUME_LIB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules/@youme/voice_engine/libs)
link_directories(${YOUME_LIB_PATH}/${CMAKE_OHOS_ARCH_ABI})
add_library(entry SHARED napi_init.cpp youme_native.cpp)
target_link_libraries(entry PUBLIC libyoume_voice_engine.so libace_napi.z.so libhilog_ndk.z.so)
应用C++代码引入SDK头文件即可调用C++ API。
#include "IYouMeVoiceEngine.h"
#include "IYouMeEventCallback.h"
#include "YouMeConstDefine.h"
// 定义SDK回调监听类
class YouMeCallback:
public IYouMeEventCallback,
public IRestApiCallback,
public IYouMeMemberChangeCallback,
public IYouMeAVStatisticNewCallback,
public IYouMeChannelMsgCallback,
public IYouMePcmCallback,
public IYouMeDetectNetworkCallback
{
public:
void onEvent(const YouMeEvent event, const YouMeErrorCode error, const char * channel, const char * param);
void onRequestRestAPI(int requestID, const YouMeErrorCode &iErrorCode, const char* strQuery, const char* strResult);
void onDetectNetWorkCompleteCb(bool bNetworkOK, const char* strResult);
void onAVStatisticNew(YouMeAVStatisticType type, const char* userID, int value, const char* param);
void onMemberChange(const char* channel, const char* listMemberChange, bool bUpdate);
void onBroadcast(const YouMeBroadcast bc, const char* channel, const char* param1, const char* param2, const char* strContent);
void onPcmDataRemote(int channelNum, int samplingRateHz, int bytesPerSample, void* data, int dataSizeInByte);
void onPcmDataRecord(int channelNum, int samplingRateHz, int bytesPerSample, void* data, int dataSizeInByte);
void onPcmDataMix(int channelNum, int samplingRateHz, int bytesPerSample, void* data, int dataSizeInByte);
}
// 设置回调
YouMeCallback* inst = new YouMeCallback();
IYouMeVoiceEngine::getInstance()->setRestApiCallback(inst);
IYouMeVoiceEngine::getInstance()->setMemberChangeCallback(inst);
IYouMeVoiceEngine::getInstance()->setNotifyCallback(inst);
IYouMeVoiceEngine::getInstance()->setDetectNetworkCallback(inst);
IYouMeVoiceEngine::getInstance()->setAVStatisticNewCallback(inst);
// 初始化SDK
YouMeErrorCode ec = IYouMeVoiceEngine::getInstance()->init(inst, appKey, appSecret, (YOUME_RTC_SERVER_REGION)serverRegion, extServerRegion);