From ac950d9d6a21a24116482b6dd0fe57b4558ebeb9 Mon Sep 17 00:00:00 2001 From: brianwernick Date: Sat, 7 Nov 2015 08:43:25 -0700 Subject: [PATCH] Added the ability to stop a VideoView from releasing resources in onDetachedFromWindow --- .../android/exomedia/EMVideoView.java | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/library/src/main/java/com/devbrackets/android/exomedia/EMVideoView.java b/library/src/main/java/com/devbrackets/android/exomedia/EMVideoView.java index 51ace531..fa483185 100644 --- a/library/src/main/java/com/devbrackets/android/exomedia/EMVideoView.java +++ b/library/src/main/java/com/devbrackets/android/exomedia/EMVideoView.java @@ -108,6 +108,7 @@ public static VideoType get(Uri uri) { private EMListenerMux listenerMux; private boolean playRequested = false; + private boolean releaseOnDetachFromWindow = true; @Nullable private EMEventBus bus; @@ -254,18 +255,10 @@ private RenderBuilder getRendererBuilder(VideoType renderType, Uri uri, MediaUti @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); + pause(); - defaultControls = null; - stopPlayback(); - overriddenPositionStopWatch.stop(); - - if (emExoPlayer != null) { - emExoPlayer.release(); - } - - if (audioCapabilitiesReceiver != null) { - audioCapabilitiesReceiver.unregister(); - audioCapabilitiesReceiver = null; + if (releaseOnDetachFromWindow) { + release(); } } @@ -295,6 +288,40 @@ public void onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities) { } } + /** + * WARNING: Use of this method may cause memory leaks. + *

+ * Enables or disables the automatic release when the EMVideoView is detached + * from the window. Normally this is expected to release all resources used + * by calling {@link #release()}. If releaseOnDetach is disabled + * then {@link #release()} will need to be manually called. + * + * @param releaseOnDetach False to disable the automatic release in {@link #onDetachedFromWindow()} + */ + public void setReleaseOnDetachFromWindow(boolean releaseOnDetach) { + this.releaseOnDetachFromWindow = releaseOnDetach; + } + + /** + * Stops the playback and releases all resources attached to this + * EMVideoView. This should not be called manually unless + * {@link #setReleaseOnDetachFromWindow(boolean)} has been set. + */ + public void release() { + defaultControls = null; + stopPlayback(); + overriddenPositionStopWatch.stop(); + + if (emExoPlayer != null) { + emExoPlayer.release(); + } + + if (audioCapabilitiesReceiver != null) { + audioCapabilitiesReceiver.unregister(); + audioCapabilitiesReceiver = null; + } + } + /** * Retrieves the user agent that the EMVideoView will use when communicating * with media servers