This document describes how to enter a room as audience and start co-anchoring. The process of entering a room and publishing streams as an anchor in live streaming scenarios is the same as that in call scenarios. Please see Real-Time Audio/Video Call.
You can click Demo to try out the audio/video features, or log in to GitHub to get the sample code related to this document.
Create a client object using TRTC.createClient(). Set the parameters as follows:
mode
: TRTC mode, which should be set to live
sdkAppId
: the sdkAppId
you obtain from Tencent ClouduserId
: user IDuserSig
: user signatureconst client = TRTC.createClient({
mode: 'live',
sdkAppId,
userId,
userSig
});
Call Client.join() to enter a TRTC room. Below are the request parameters:
roomId
: room IDrole
: roleanchor
(default): users in the role of “anchor” can publish local streams and play remote streams.audience
. Users in the role of “audience” can play remote streams but cannot publish local streams. To co-anchor and publish local streams, audience must switch the role to anchor
using Client.switchRole().// Enter a room as audience
client
.join({ roomId, role: 'audience' })
.then(() => {
console.log('Entered room successfully');
})
.catch(error => {
console.error('Failed to enter room' + error);
});
client.on('stream-added')
, which is used to listen for remote streams, call Client.subscribe() to subscribe to the remote stream.
Note:To ensure that you are notified when a remote user enters the room, please subscribe to the
client.on('stream-added')
callback before you call Client.join() to enter the room.
client.on('stream-added', event => {
const remoteStream = event.stream;
console.log('New remote stream:' + remoteStream.getId());
// Subscribe to the remote stream
client.subscribe(remoteStream);
});
client.on('stream-subscribed', event => {
const remoteStream = event.stream;
console.log('Subscribed to remote stream successfully:' + remoteStream.getId());
// Play the remote stream
remoteStream.play('remote_stream-' + remoteStream.getId());
});
client.on('stream-subscribed', event => {
const remoteStream = event.stream;
console.log('Subscribed to remote stream successfully:' + remoteStream.getId());
// Play the remote stream
remoteStream.play('remote_stream-' + remoteStream.getId());
});
Call Client.switchRole() to switch the role to anchor
.
client
.switchRole('anchor')
.then(() => {
// Role switched to “anchor” successfully
})
.catch(error => {
console.error('Failed to switch role' + error);
});
userId
: ID of the user to whom the local stream belongs
audio
: whether to enable audio
video
: whether to enable video
const localStream = TRTC.createStream({ userId, audio: true, video: true });
localStream
.initialize()
.then(() => {
console.log('Local stream initialized successfully');
})
.catch(error => {
console.error('Failed to initialize local stream' + error);
});
localStream
.initialize()
.then(() => {
console.log('Local stream initialized successfully');
localStream.play('local_stream');
})
.catch(error => {
console.error('Failed to initialize local stream' + error);
});
client
.publish(localStream)
.then(() => {
console.log('Local stream published successfully');
})
.catch(error => {
console.error('Failed to publish local stream' + error);
});
Call Client.leave() to exit the room. The live streaming session ends.
client
.leave()
.then(() => {
// Exited room successfully
})
.catch(error => {
console.error('Failed to leave room' + error);
});
Note:The value of
appScene
must be the same on each client. InconsistentappScene
may cause unexpected problems.
Was this page helpful?