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.
aliRtcEngine.setChannelProfile(AliRTCSDK_Channel_Profile.AliRTCSDK_Interactive_live); - Disable automatic publishing and enable automatic subscription for media tracks in the channel.
aliRtcEngine.setAutoPublishSubscribe(false, true); - Set AliRTCSDK_Client_Role to AliRTCSDK_Interactive.
aliRtcEngine.setClientRole(AliRTCSDK_Client_Role.AliRTCSDK_Interactive);Valid values for AliRTCSDK_Client_Role:
- AliRTCSDK_Interactive: a role that can publish and subscribe to media tracks.
- AliRTCSDK_live: 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.
aliRtcEngine.joinChannel(aliRtcAuthInfo, userName); - Call the publish method to publish media tracks.
// Publish audio tracks based on your needs. aliRtcEngine.configLocalAudioPublish(true); // Publish camera tracks based on your needs. aliRtcEngine.configLocalCameraPublish(true); // Publish screen tracks based on your needs. aliRtcEngine.configLocalScreenPublish(true); // Publish media tracks. The publish method is asynchronous. If the onPublishResult callback returns 0, the media tracks are published. You can call setRtcEngineEventListener to configure onPublishResult. aliRtcEngine.publish();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.
/** * This callback message is returned when a remote user publishes media tracks. * @note This callback message is also returned when a user unpublishes media tracks. * @param uid User ID * @param audioTrack The publishing status of the audio track. * @param videoTrack The publishing status of the video track. */ void onRemoteTrackAvailableNotify(String uid, AliRtcAudioTrack audioTrack, 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 onUpdateRoleNotify(AliRtcEngine.AliRTCSDK_Client_Role oldRole, AliRtcEngine.AliRTCSDK_Client_Role 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 the logic for publishing and subscription of media tracks.
aliRtcEngine.setClientRole(AliRTCSDK_Client_Role.AliRTCSDK_Interactive);
Student side
Set the channel to the interactive mode.
AliRtcEngine aliRtcEngine = AliRtcEngine.getInstance(getApplicationContext());
aliRtcEngine.setChannelProfile(AliRTCSDK_Channel_Profile.AliRTCSDK_Interactive_live);Viewers: After you set the channel mode to interactive, 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 AliRTCSDK_Interactive before you call the publish method.
aliRtcEngine.setClientRole(AliRTCSDK_Client_Role.AliRTCSDK_Interactive);After you change the user role, the user receives a callback./** * The callback for changing roles. This callback is returned after you call setClientRole to change the role of a user in the channel. * * @ param oldRole The old role of the user. * @ param newRole The new role of the user. * @ return */ void onUpdateRoleNotify(AliRtcEngine.AliRTCSDK_Client_Role oldRole, AliRtcEngine.AliRTCSDK_Client_Role newRole); - Call the publish method to publish media tracks after the callback is returned.
// Publish audio tracks based on your needs. aliRtcEngine.configLocalAudioPublish(true); // Publish camera tracks based on your needs. aliRtcEngine.configLocalCameraPublish(true); // Publish screen tracks based on your needs. aliRtcEngine.configLocalScreenPublish(true); // Publish media tracks. The publish method is asynchronous. If 0 is returned after you call onPublishResult, the media tracks are published. You can call setRtcEngineEventListener to configure callbacks for onPublishResult. aliRtcEngine.publish();
- 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.
// Unpublish audio tracks based on your needs. aliRtcEngine.configLocalAudioPublish(false); // Unpublish camera tracks based on your needs. aliRtcEngine.configLocalCameraPublish(false); // Unpublish screen tracks based on your needs. aliRtcEngine.configLocalScreenPublish(false); // Unpublish media tracks. The publish method is asynchronous. If 0 is returned after you call onPublishResult, the media tracks are published. You can call setRtcEngineEventListener to configure callbacks for onPublishResult. aliRtcEngine.publish(); - After the callback for unpublishing media tracks is returned, change the user role from participant to viewer.
aliRtcEngine.setClientRole(AliRTCSDK_Client_Role.AliRTCSDK_live); - After the callback for changing the role is returned, the viewer can subscribe to media tracks in the channel.
- Unpublish media tracks.
该文章对您有帮助吗?