Talk SDK for Harmony 典型实现方案
实现语音通话
相关接口
- SetEventCallback:注册SDK事件监听回调,需SDK初始化之前设置
- Init:初始化SDK
- JoinChannelSingleMode:加入语音频道(单频道)
- SetSpeakerMute:设置扬声器状态
- SetMicrophoneMute:设置麦克风状态
- LeaveChannelAll:离开频道
- UnInit:反初始化SDK
实现步骤
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`
2. 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}`);
});
3. 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;
}
}
}
4. 加入语音频道
let error = YouMeRtcAPI.Instance().JoinChannelSingleMode(this.userId, this.channelId, this.selectUserRole, false);
if(error == 0) {
hilog.info(logFlag, logTag, `join channel single succ, user:${this.userId} channel:${this.channelId}`);
this.appendEventInfo(`【api】加入语音频道成功,用户:${this.userId} 频道:${this.channelId} 区域:${this.selectedRegionVal}`);
} else {
hilog.error(logFlag, logTag, `join channel single err:${error} user:${this.userId} channel:${this.channelId}`);
this.appendEventInfo(`【api】加入语音频道失败,错误码:${error} 用户:${this.userId} 频道:${this.channelId} 区域:${this.selectedRegionVal}`)
}
5. 打开扬声器和麦克风
YouMeRtcAPI.Instance().SetMicMute(this.isMicMute)
YouMeRtcAPI.Instance().SetSpeakerMute(this.isSpeakerMute)
6. 离开频道
Button('退出频道')
.borderRadius('0px')
.enabled(this.sdkInitialized && this.sdkUserLogin)
.width('100%')
.onClick(() => {
let error = YouMeRtcAPI.Instance().LeaveChannelAll();
if(error == 0) {
hilog.info(logFlag, logTag, `leave channel all succ`);
this.appendEventInfo(`【api】退出语音频道成功`);
} else {
hilog.info(logFlag, logTag, `leave channel all err:${error}`);
this.appendEventInfo(`【api】退出语音频道失败,错误码:${error}`)
}
})
7. 反初始化SDK
Button('反初始化')
.borderRadius('0px')
.width('100%')
.enabled(this.sdkInitialized)
.onClick(() => {
const promise = new Promise<number>((resolve, reject) => {
let error = YouMeRtcAPI.Instance().UnInit();
if (error == 0) {
resolve(error);
} else {
reject(new Error("error call failed"));
}
});
promise
.then(() => {
this.sdkInitStatus = `SDK未初始化`;
this.sdkInitialized = false
this.sdkUserLogin = false
this.appendEventInfo(`【api】sdk反初始化成功`)
})
.catch((e: Error) => {
hilog.error(logFlag, logTag, 'uninit sdk err:%{public}s', e.toString());
this.appendEventInfo(`【api】sdk反初始化失败:${e.toString()}`)
});
})