mode:互动直播模式,设置为 livesdkAppId:您从腾讯云申请的 sdkAppIduserId:用户 IDuserSig:用户签名const client = TRTC.createClient({mode: 'live',sdkAppId,userId,userSig});
roomId:房间 IDrole:用户角色anchor:主播,主播角色具有发布本地流和接收远端流的权限。默认是主播角色。audience:观众,观众角色只有接收远端流的权限,没有发布本地流的权限。若观众想要跟主播连麦互动,需要通过 Client.switchRole() 切换角色至anchor主播角色后再发布本地流。// 以观众角色进房收看client.join({ roomId, role: 'audience' }).then(() => {console.log('进房成功');}).catch(error => {console.error('进房失败 ' + error);});
client.on('stream-added') 获取,收到该事件后,通过 Client.subscribe() 订阅远端音视频流。client.on('stream-added', event => {const remoteStream = event.stream;console.log('远端流增加: ' + remoteStream.getId());//订阅远端流client.subscribe(remoteStream);});client.on('stream-subscribed', event => {const remoteStream = event.stream;console.log('远端流订阅成功:' + remoteStream.getId());// 播放远端流remoteStream.play('remote_stream-' + remoteStream.getId());});
play 方法接受一个 div 元素 ID 作为参数,SDK 内部会在该 div 元素下自动创建相应的音视频标签并在其上播放音视频。client.on('stream-subscribed', event => {const remoteStream = event.stream;console.log('远端流订阅成功:' + remoteStream.getId());// 播放远端流remoteStream.play('remote_stream-' + remoteStream.getId());});
client.switchRole('anchor').then(() => {// 角色切换成功,现在是主播角色}).catch(error => {console.error('角色切换失败 ' + error);});
userId:本地流所属用户 IDaudio:是否开启音频video:是否开启视频const localStream = TRTC.createStream({ userId, audio: true, video: true });
localStream.initialize().then(() => {console.log('初始化本地流成功');}).catch(error => {console.error('初始化本地流失败 ' + error);});
localStream.initialize().then(() => {console.log('初始化本地流成功');localStream.play('local_stream');}).catch(error => {console.error('初始化本地流失败 ' + error);});
client.publish(localStream).then(() => {console.log('本地流发布成功');}).catch(error => {console.error('本地流发布失败 ' + error);});
client.leave().then(() => {// 退房成功}).catch(error => {console.error('退房失败 ' + error);});
文档反馈