Skip to content

Commit

Permalink
Added the ability to stop a VideoView from releasing resources in onD…
Browse files Browse the repository at this point in the history
…etachedFromWindow
  • Loading branch information
brianwernick committed Nov 7, 2015
1 parent 89aaad8 commit ac950d9
Showing 1 changed file with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -295,6 +288,40 @@ public void onAudioCapabilitiesChanged(AudioCapabilities audioCapabilities) {
}
}

/**
* <b><em>WARNING:</em></b> Use of this method may cause memory leaks.
* <p>
* 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 <code>releaseOnDetach</code> 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
Expand Down

0 comments on commit ac950d9

Please sign in to comment.