预先说明: 由于观众端也可以推流成为主播,为了避免歧义,下文中的观众均指为
ZEGO_AUDIENCE
的角色,主播均指ZEGO_ANCHOR
的角色。
SDK 提供了主播和观众的连麦(多主播同台)功能,支持主播与观众互动。直播中的连麦分为两种情况:
下面分别介绍这两种方式的实现。
连麦操作的前提是主播正在推流、观众正在同一房间拉流观看。因此前置步骤同推拉流一致,此处不再赘述。
参考文档:快速集成-推流
参考文档:快速集成-拉流
观众申请连麦的流程为:
流程图如下所示:
按照上述步骤,SDK API 调用过程如下所示。
请特别注意:
下述处理流程中,请开发者注意区分各 API 是属于观众端逻辑,还是主播端逻辑。否则可能导致数据流不通,功能异常。
开发者需要在观众端调用此 requestJoinLive 实现连麦请求。主播端的响应结果会通过 onRecvJoinLiveRequest 回调。
zg.requestJoinLive(destIdName, function(req) {
console.log('requestJoinLive suc', seq);
}, function(err, seq) {
console.log('requestJoinLive err', err, seq);
}, function(result, fromUserId, fromUserName) {
console.log('requestJoinLive callback', result, fromUserId, fromUserName);
//预览推流
} )
在主播端通过onRecvJoinLiveRequest处理收到的连麦请求,通过赋值的方式在推拉流之前设置该回调
观众连麦请求发送成功后,主播端可直接在此 API 中收到观众端的连麦申请。
zg.onRecvJoinLiveRequest = function (requestId, from_userid, from_username, roomid) {
console.log('onRecvJoinLiveRequest', requestId, from_userid, from_username, roomid);
//响应连麦
zg.respondJoinLive(requestId, true, function(seq) {
console.log('respondJoinLive success', seq);
},function(err, seq) {
console.log('respondJoinLive err', err, seq);
})
}
主播端收到观众端的连麦请求后,需要调用respondJoinLive做出响应(同意或拒绝)。
一般是在 3.2 onRecvJoinLiveRequest
的通知中调用此 API 响应。
//同意连麦
zg.respondJoinLive(requestId, true, function(seq) {
console.log('respondJoinLive success', seq);
},function(err, seq) {
console.log('respondJoinLive err', err, seq);
})
观众端可直接在 3.1 requestJoinLive
的第三个回调中获取主播端的响应结果,无需额外调用其他 API。
连麦结束后,开发者需要在主播端调用此 endJoinLive结束与指定观众的连麦。
zg.endJoinLive(destIdName, function(seq){
console.log('requestJoinLive suc', seq);
}, function(err, seq) {
console.log('requestJoinLive err', err, seq);
})
除此之外,观众也可结束连麦,调用 stopPublishingStream
即可。
主播端结束连麦后,观众端可在此onRecvEndJoinLiveCommand中获取结束连麦的通知,并在其中进行一些清理操作。
onRecvEndJoinLiveCommand: function (requestId, from_userid, from_username, roomid) {
console.log('onRecvEndJoinLiveCommand', requestId, from_userid, from_username, roomid);
//一些清理操作,例如停止预览推流
}
此步骤仅适用于主播结束连麦,观众调用 stopPublishingStream
结束连麦,主播方不支持类似回调,但可以通过房间流的删减知悉。
主播邀请观众连麦的流程为:
流程图如下所示:
按照上述步骤,SDK API 调用过程如下。
请特别注意:
下述处理流程中,请开发者注意区分各 API 是属于观众端逻辑,还是主播端逻辑。否则可能导致数据流不通,功能异常。
开发者需要在主播端调用inviteJoinLive实现连麦邀请,邀请的观众必须是同一房间内的观众。观众端的响应结果会通过其中的 onRecvInviteJoinLiveRequest
回调。
zg.inviteJoinLive(destIdName, function(seq) {
console.log('inviteJoinLive suc', seq);
}, function(err, seq) {
console.log('inviteJoinLive err', err, seq);
}, function(result, fromUserId, fromUserName) {
//预览推流
})
主播连麦邀请发送成功后,被邀请的观众端可直接在此onRecvInviteJoinLiveRequest 中收到主播端的连麦请求。
zg.onRecvInviteJoinLiveRequest = function(requestId, from_userid, from_username, roomid) {
console.log('onRecvInviteJoinLiveRequest', requestId, from_userid, from_username, roomid);
//同意连麦
zg.respondJoinLive(requestId, true, function(seq) {
console.log('respondJoinLive success', seq);
//预览推流相关操作
},function(err, seq) {
console.log('respondJoinLive err', err, seq);
})
}
观众端收到主播端的连麦邀请后,需要调用此 respondJoinLive 做出响应(同意或拒绝)。一般是在 4.2 onRecvInviteJoinLiveRequest
的通知中调用此 API
响应。
zg.respondJoinLive(requestId, true, function(seq) {
console.log('respondJoinLive success', seq);
//预览推流相关操作
},function(err, seq) {
console.log('respondJoinLive err', err, seq);
})
主播端可直接在 4.1 inviteJoinLive
的第三个回调中获取观众端的响应结果,无需额外调用其他 API。
连麦结束后,开发者需要在主播端调用endJoinLive结束与指定观众的连麦。
zg.endJoinLive(destIdName, function(seq){
console.log('requestJoinLive suc', seq);
}, function(err, seq) {
console.log('requestJoinLive err', err, seq);
})
除此之外,观众也可结束连麦,调用 stopPublishing
即可。
主播端结束连麦后,观众端可在此onRecvEndJoinLiveCommand中获取结束连麦的通知,并在其中进行一些清理操作。
onRecvEndJoinLiveCommand: function (requestId, from_userid, from_username, roomid) {
console.log('onRecvEndJoinLiveCommand', requestId, from_userid, from_username, roomid);
//一些清理操作,例如停止预览推流
}
此步骤仅适用于主播结束连麦,观众调用 stopPublishing
结束连麦,主播方不支持类似回调,但可以通过房间流的删减知悉。
连麦系统架构图如下所示:
联系我们
文档反馈