@@ -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
@@ -382,46 +430,7 @@ public void onHostPause() {
382
430
383
431
@ Override
384
432
public void onHostDestroy () {
385
- /*
386
- * Remove stream voice control
387
- */
388
- if (themedReactContext .getCurrentActivity () != null ) {
389
- themedReactContext .getCurrentActivity ().setVolumeControlStream (AudioManager .USE_DEFAULT_STREAM_TYPE );
390
- }
391
- /*
392
- * Always disconnect from the room before leaving the Activity to
393
- * ensure any memory allocated to the Room resource is freed.
394
- */
395
- if (room != null && room .getState () != Room .State .DISCONNECTED ) {
396
- room .disconnect ();
397
- disconnectedFromOnDestroy = true ;
398
- }
399
-
400
- /*
401
- * Release the local media ensuring any memory allocated to audio or video is freed.
402
- */
403
- if (localVideoTrack != null ) {
404
- localVideoTrack .release ();
405
- localVideoTrack = null ;
406
- }
407
-
408
- if (localAudioTrack != null ) {
409
- localAudioTrack .release ();
410
- localAudioTrack = null ;
411
- }
412
-
413
- // Quit the data track message thread
414
- dataTrackMessageThread .quit ();
415
-
416
-
417
- }
418
-
419
- public void releaseResource () {
420
- themedReactContext .removeLifecycleEventListener (this );
421
- room = null ;
422
- localVideoTrack = null ;
423
- thumbnailVideoView = null ;
424
- cameraCapturer = null ;
433
+ releaseResource ();
425
434
}
426
435
427
436
// ====== CONNECTING ===========================================================================
0 commit comments