@@ -208,8 +208,56 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
208
208
private final Map <RemoteDataTrack , RemoteParticipant > dataTrackRemoteParticipantMap =
209
209
new HashMap <>();
210
210
211
+ public void releaseResource () {
212
+ thumbnailVideoView = null ;
213
+ roomName = null ;
214
+ accessToken = null ;
215
+
216
+ /*
217
+ * Remove stream voice control
218
+ */
219
+ if (themedReactContext != null && themedReactContext .getCurrentActivity () != null ) {
220
+ themedReactContext .getCurrentActivity ().setVolumeControlStream (AudioManager .USE_DEFAULT_STREAM_TYPE );
221
+ themedReactContext .removeLifecycleEventListener (this );
222
+ }
223
+ /*
224
+ * Always disconnect from the room before leaving the Activity to
225
+ * ensure any memory allocated to the Room resource is freed.
226
+ */
227
+ if (room != null && room .getState () != Room .State .DISCONNECTED ) {
228
+ room .disconnect ();
229
+ disconnectedFromOnDestroy = true ;
230
+ }
231
+ room = null ;
232
+
233
+
234
+ if (localParticipant != null ) {
235
+ localParticipant .unpublishTrack (localVideoTrack );
236
+ localParticipant = null ;
237
+ }
238
+
239
+ if (localVideoTrack != null ) {
240
+ localVideoTrack .release ();
241
+ localVideoTrack = null ;
242
+ }
243
+
244
+ if (localAudioTrack != null ) {
245
+ localAudioTrack .release ();
246
+ localAudioTrack = null ;
247
+ }
248
+
249
+ if (cameraCapturer != null ) {
250
+ cameraCapturer .stopCapture ();
251
+ cameraCapturer = null ;
252
+ }
253
+
254
+ // Quit the data track message thread
255
+ dataTrackMessageThread .quit ();
256
+ }
257
+
211
258
public CustomTwilioVideoView (ThemedReactContext context ) {
212
259
super (context );
260
+ releaseResource ();
213
261
this .themedReactContext = context ;
214
262
this .eventEmitter = themedReactContext .getJSModule (RCTEventEmitter .class );
215
263
@@ -385,40 +433,7 @@ public void onHostPause() {
385
433
386
434
@ Override
387
435
public void onHostDestroy () {
388
- /*
389
- * Always disconnect from the room before leaving the Activity to
390
- * ensure any memory allocated to the Room resource is freed.
391
- */
392
- if (room != null && room .getState () != Room .State .DISCONNECTED ) {
393
- room .disconnect ();
394
- disconnectedFromOnDestroy = true ;
395
- }
396
-
397
- /*
398
- * Release the local media ensuring any memory allocated to audio or video is freed.
399
- */
400
- if (localVideoTrack != null ) {
401
- localVideoTrack .release ();
402
- localVideoTrack = null ;
403
- }
404
-
405
- if (localAudioTrack != null ) {
406
- localAudioTrack .release ();
407
- localAudioTrack = null ;
408
- }
409
-
410
- // Quit the data track message thread
411
- dataTrackMessageThread .quit ();
412
-
413
-
414
- }
415
-
416
- public void releaseResource () {
417
- themedReactContext .removeLifecycleEventListener (this );
418
- room = null ;
419
- localVideoTrack = null ;
420
- thumbnailVideoView = null ;
421
- cameraCapturer = null ;
436
+ releaseResource ();
422
437
}
423
438
424
439
// ====== CONNECTING ===========================================================================
0 commit comments