This topic describes how to use Real-Time Communication (RTC) SDK to host an interactive class between a teacher and students.
Teacher side
- Set the channel to the interactive mode.
[self.engine setChannelProfile:AliRtcChannelProfileInteractivelive]; - Disable automatic publishing and enable automatic subscription for media tracks in the channel.
[self.engine setAutoPublish:NO withAutoSubscribe:YES]; - Set AliRtcClientRole to AliRtcClientRoleInteractive.
[self.engine setClientRole:AliRtcClientRoleInteractive];Valid values for AliRtcClientRole:- AliRtcClientRoleInteractive: a role that can publish and subscribe to media tracks.
- AliRtcClientRolelive: a role that can only subscribe to media tracks. Assign this role to viewers.
Note Ignore the return value of setClientRole. - The teacher joins the channel.
[self.engine joinChannel:authInfo name:name onResult:^(NSInteger errCode){ if (errCode == 0) { // Operation succeeded. } else { // Operation failed. } }]; - Call the publish method to publish media tracks.
[self.engine configLocalAudioPublish:YES]; [self.engine configLocalCameraPublish:YES]; [self.engine publish:^(int err) { }];Important Call the publish method after the teacher joins the channel. Otherwise, the call fails. - Obtain the callbacks for publishing and unpublishing media tracks. The teacher and students receive the same callbacks.
/** * @brief This callback message is returned when a user publishes media tracks. * @note This callback message is also returned when a user unpublishes media tracks. */ - (void)onRemoteTrackAvailableNotify:(NSString *)uid audioTrack:(AliRtcAudioTrack)audioTrack videoTrack:(AliRtcVideoTrack)videoTrack;Note- audioTrack indicates the publishing status of the audio track and videoTrack indicates the publishing status of the video track. If AliRtcAudioTrackNo is returned for both audioTrack and videoTrack, the media tracks are unpublished.
- You can configure roles for users after they join the channel. Users receive a callback after the configurations are complete.
- (void)onUpdateRoleNotifyWithOldRole:(AliRtcClientRole)oldRole newRole:(AliRtcClientRole)newRole; - Take note of the user role before you call the following method to change roles. For example, no callback is returned after you call the following method to change the AliRtcClientRoleInteractive role of a user. We recommend that you separate the logic for changing roles from publishing and subscription of media tracks.
[self.engine setClientRole:AliRtcClientRoleInteractive];
Student side
Set the channel to the interactive mode.
[self.engine setChannelProfile:AliRtcInteractivelive];Viewers: After you set the channel to the interactive mode, the viewers in the channel can only subscribe to the media tracks. Assign this role to viewers who do not need to publish media tracks.
Participants: The participants in the channel can publish media tracks. The following section describes how participants publish and unpublish media tracks.
- The student turns on mic.
- Change the user role from viewer to participant to allow the user to participate in online interaction.
- Change the role to AliRtcClientRoleInteractive before you call the publish method.
[self.engine setClientRole:AliRtcClientRoleInteractive];After you change the user role, the user receives a callback.- (void)onUpdateRoleNotifyWithOldRole:(AliRtcClientRole)oldRole newRole:(AliRtcClientRole)newRole; - Call the publish method to publish media tracks after the callback is returned.
[self.engine configLocalAudioPublish:YES]; [self.engine configLocalCameraPublish:YES]; [self.engine publish:^(int err) { }];
- The student turns off mic.
Unpublish media tracks and call the publish method to change the user role from participant to viewer. Then, the user can turn off the mic.
- Unpublish media tracks.
[self.engine configLocalAudioPublish:NO]; [self.engine configLocalCameraPublish:NO]; [self.engine publish:^(int err) { }]; - After the callback for unpublishing media tracks is returned, change the user role to viewer.
[self.engine setClientRole:AliRtcClientRolelive]; - After you change the user role, the user receives a callback.
- (void)onUpdateRoleNotifyWithOldRole:(AliRtcClientRole)oldRole newRole:(AliRtcClientRole)newRole;
- Unpublish media tracks.
该文章对您有帮助吗?