Handle a Local Stream
The Android SDK provides the following methods to handle a local stream:
- switchCameraPreview(): To switch Camera Feed preview.
- publish(): To publish a local stream in the connected room.
- unpublish(): To disconnect a local stream from the room.
- switchCamera(): To switch the camera.
- switchMediaDevice(): To switch both camera and microphone.
- muteSelfAudio(): To mute the audio track of a local stream.
- muteSelfVideo(): To mute the video track of a local stream.
Switch Camera preview
This is to switch between Camera Feed to preview.
Class: EnxStream
Method: public void switchCameraPreview
Sample Code
stream.switchCameraPreview();
Publish a Local Stream
It is important to publish your media stream into the room for other connected users to see and communicate with you. You can continue publishing your stream until you exit, or unpublish and republish your media stream multiple times within the same session if required.
After initializing the stream, you need to publish it in the room so that other users in the room can see and hear the user on this stream.
The EnxRoom.publish()
method publishes a local stream in the connected room.
Class: EnxRoom
Method: public void publish(EnxStream localStream)
Parameter
localStream
: JSON Object. The initiated stream object.
Observers
Observer | Description |
---|---|
onPublishedStream | Acknowledgment to the stream publisher when the stream gets published. |
onStreamAdded | Notification to everyone in a room when a new stream is published in the room. |
Sample Code
room.publish(localStream); // To publish initialized Local Streampublic void onPublishedStream(EnxStream stream) {// Your Stream is published}public void onStreamAdded(EnxStream stream) {// Announcement of a new Stream in the Room// You may subscribe to the Stream}
Error Codes and Exceptions
Code | Description |
---|---|
5013 | Failed to publish stream. |
5015 | Unable to publish a stream without audio, video, and data. |
5016 | Unable to publish a stream with illegible attributes. |
5022 | Failed to publish stream because floor access not received in lecture mode room. |
5023 | The stream has already been published. |
5024 | Repeated publish() call while stream publishing is in process. |
5025 | Failed to publish stream because publish() call is made on a remote stream. |
1170 | The current subscription doesn't support the publishing of stream. |
Unpublish a Local Stream
The EnxRoom.unpublish()
method disconnects the local stream from the room. Unpublishing a stream does not release the device access, so subsequent republishing does not require device access permission.
Class: EnxRoom
Method: public void unpublish()
Observer
onRemoveStream
: Notification to everyone in the room when a stream is removed from the room.
Sample Code
room.unpublish(); // To unpublish all Local Streamspublic void onRemoveStream(EnxStream stream) {// To announce in the room that a stream has been removed from the room.}
Error Codes and Exceptions
Code | Description |
---|---|
5029 | Repeated unpublish() is called when the previous unpublish request is in process. |
5030 | Non-contextual method is called when the user tries to unpublish a stream before publishing it. |
Switch Camera, Microphone, and Speaker
You may need to switch to an alternate camera, microphone, or speaker while being in a session without any break in the communication. APIs helps you achieve this seamlessly.
The following APIs allow you to switch to alternate media devices after publishing a stream, thus facilitating a switch on the fly.
Switch between Rear and Front Camera
The EnxStream.switchCamera()
method allows you to switch between the rear and front camera as a source for the published stream.
Class: EnxStream
Method: public void switchCamera()
Sample Code
localStream.switchCamera();
Error Codes and Exceptions
Code | Description |
---|---|
5021 | Unable to switch camera in audio-only call mode. |
5097 | Unable to switch camera in a chat-only room. Non-contextual method call. |
Switch to Alternate Microphone
The EnxRoom.switchMediaDevice()
method switches the audio source of the published stream to an alternate microphone. This method requires the ID of the audio device to switch to, which needs to be retrieved using EnxRoom.getDevices()
method.
Class: EnxRoom
Method: public void switchMediaDevice(String micDeviceId)
Parameter
micDeviceId
: String. Target microphone device ID.
Observer
onNotifyDeviceUpdate
: Acknowledgment sent with the new audio device ID after audio device update is complete.
Sample Code
room.switchMediaDevice( micDeviceId );public void onNotifyDeviceUpdate( String micDeviceId ) {// Switched to micDeviceId}
Mute or Unmute Audio in a Stream
The EnxStream.muteSelfAudio()
method is used to mute/unmute audio of your local stream.
Class: EnxStream
Method: public void muteSelfAudio( isMute )
Parameter
isMute
: Boolean. Set it to true to mute audio; Set it to false to unmute audio.
Callbacks
Callback | Description |
---|---|
onRemoteStreamAudioMute | Notification to everyone in the room when a user mutes self audio. |
onRemoteStreamAudioUnMute | Notification to everyone in the room when a user unmutes self audio. |
onAudioEvent | Acknowledgment to the user when self audio is muted/unmuted. |
Sample Code
localStream.muteSelfAudio(true); // To mute audio of local streamlocalStream.muteSelfAudio(true); // To unmute audio of local stream// To self. Audio is muted/unmuted.public void onAudioEvent(JSONObject json) {// json { "result":0, "msg": "Audio Off" }// json { "result":0, "msg": "Audio On" }}// To all. Audio muted by Remote user.public void onRemoteStreamAudioMute(JSONObject json) {// json {"result":0, "msg":"User muted audio", "clientId": "XXX" }}// To all. Audio unmuted by Remote user.public void onRemoteStreamAudioUnMute(JSONObject json) {// json {"result":0, "msg":"User unmuted audio", "clientId": "XXX" }}
Error Codes and Exceptions
Code | Description |
---|---|
5058 | Repeated muteSelfAudio() is called when a previous mute request is in process. |
5059 | Repeated muteSelfAudio() is called when a previous unmute request is in process. |
5060 | Repeated muteSelfAudio() is called after audio has been muted already. |
5061 | Trying to unmute audio without muting it first. |
5062 | Unable to unmute audio as audio hard-muted by the moderator. |
Mute or Unmute Video in a Stream
The EnxStream.muteSelfVideo()
method is used to mute/unmute the video of a local stream.
Class: EnxStream
Method: public void muteSelfVideo( isMute )
Parameter
isMute
: Boolean. Set it to true to mute the video; otherwise, set it to false.
Callbacks
Callback | Description |
---|---|
onRemoteStreamVideoMute | Notification to everyone in the room when a user mutes self video. |
onRemoteStreamVideoUnMute | Notification to everyone in the room when a user unmutes self video. |
onVideoEvent | Acknowledgment to the user when self video is muted/unmuted. |
Sample Code
localStream.muteSelfVideo(true); // To mute video of local streamlocalStream.muteSelfVideo(false); // To unmute video of local stream// To self. Video is muted/unmuted.public void onVideoEvent(JSONObject json) {// json { "result":0, "msg": "Video Off" }// json { "result":0, "msg": "Video On" }}// To all. Video muted by Remote userpublic void onRemoteStreamVideoMute(JSONObject json) {// json {"result":0, "msg":"User muted video", "clientId": "XXX" }}// To all. Video unmuted by Remote userpublic void onRemoteStreamVideoUnMute(JSONObject json) {// json {"result":0, "msg":"User unmuted video", "clientId": "XXX" }}
Error Codes and Exceptions
Code | Description |
---|---|
5020 | Unable to process muteSelfVideo() when the user has denied camera permission. |
5071 | Repeated muteSelfVideo() call made while a previous request is in process. |
5063 | Repeated muteSelfVideo() call made after video has been muted already. |
5064 | Repeated muteSelfVideo() call made after video has been unmuted already. |
5065 | When the user tries to unmute Video without muting it first. Non-contextual method call. |
5066 | Unable to unmute video as video hard-muted by the moderator. |
5070 | Unable to unmute video in audio-only call mode. |