
回复
本文原创发布在华为开发者社区,更多鸿蒙场景化示例请见华为开发者联盟官网“行业实践与常见问题”专题页。
本示例使用声网SDK构建了一个简易的直播场景,模拟主播开、关直播以及观众进入、离开直播间时的弹幕互动。
该SDK需要下载并放在工程entry/libs目录下,SDK的使用,参考官网文档。
打开应用,分别有观众端、直播端,点击进入不同页面,进行不同操作。
声网开通服务。
添加视频渲染组件,rtc sdk使用XComponent作为视频渲染组件。
XComponent({
id: this.isLiveStreamer ? 'local' : 'remote',
type: 'surface',
libraryname: Constants.AGORA_LIB_NAME,
})
.onLoad(() => {
})
this.rtcEngine = RtcEngine.create(config);
this.rtcEngine.enableVideo();
if (this.isLiveStreamer) {
let canvas: VideoCanvas = new VideoCanvas("local");
canvas.uid = this.liveStreamerUid;
canvas.renderMode = VideoCanvas.RENDER_MODE_HIDDEN;
canvas.mirrorMode = VideoCanvas.VIDEO_MIRROR_MODE_ENABLED;
this.rtcEngine.setupLocalVideo(canvas);
this.rtcEngine.startPreview();
}
let option: ChannelMediaOptions = new ChannelMediaOptions();
option.autoSubscribeAudio = this.isLiveStreamer ? false : true;
option.autoSubscribeVideo = this.isLiveStreamer ? false : true;;
option.publishCameraTrack = this.isLiveStreamer ? true : false;;
option.publishMicrophoneTrack = this.isLiveStreamer ? true : false;;
option.channelProfile = Constants.ChannelProfile.LIVE_BROADCASTING;
option.clientRoleType =
// joinChannelWithOptions(token:string, channelId:string, uid:number, options:ChannelMediaOptions)
// uid 为0,表示由服务器分配uid,分配的uid 将通过onJoinChannelSuccess回调返回
this.rtcEngine.joinChannelWithOptions("", this.channel, this.isLiveStreamer ? this.liveStreamerUid : 0, option);
release() {
if (this.rtcEngine != null) {
this.rtcEngine!.leaveChannel();
RtcEngine.destroy();
this.rtcEngine = null;
}
}