ExoMedia icon indicating copy to clipboard operation
ExoMedia copied to clipboard

Recommended way for releasing and recreating the player?

Open saket opened this issue 8 years ago • 13 comments

  • [X ] I have verified there are no duplicate active or recent bugs, questions, or requests
Include the following:
  • ExoMedia version: 4.0.0-preview
  • Device OS version: 7.0
  • Devide Manufacturer: Google
  • Device Name: Emulator

What's the recommended way of releasing the player when the app is going in background and recreating it when it comes into foreground?

saket avatar Apr 03 '17 08:04 saket

The VideoView automatically handles releasing when appropriate (onDetachedFromWindow()) and shouldn't require any additional thought towards onStart/onStop of the parent Fragment or Activity. The AudioPlayer on the other hand should either be in a Service or be stopped/released in onStop() and created/started in onStart()

brianwernick avatar Apr 03 '17 14:04 brianwernick

Right, but onDetachedFromWindow() only gets called when the layout is being destroyed. This means that the app will continue to keep the player in memory even when it's in background.

saket avatar Apr 03 '17 14:04 saket

I don't see a reason to worry about the memory being used while in the background (in this case) as Android will inform the app of lowMemory or will tell it to destroy itself.

brianwernick avatar Apr 03 '17 14:04 brianwernick

Because Android sends a lowMemory callback only when it's really low in memory. Apps, as good citizens, should not consume memory when they are not being used. Exoplayer also suggests doing the same in its documentation.

saket avatar Apr 03 '17 14:04 saket

It’s important to release the player when it’s no longer needed, so as to free up limited resources such as video decoders for use by other applications. This can be done by calling ExoPlayer.release.

There are two cases that I see:

  1. There is no video/audio in playback
  2. Media playback was paused and the app backgrounded

In the first case we can definitely release the resources used, and this already happens with the stop methods (which are automatically called when playback completes). However with the second case the resources (memory) are still being used to cache the next segment of media for playback, performing a release would cause the media to be reloaded when the app is resumed which is unexpected.

brianwernick avatar Apr 03 '17 14:04 brianwernick

IMHO, the library should not be making this decision for the developer and should atleast let the developer change it :)

saket avatar Apr 04 '17 09:04 saket

The VideoView isn't doing anything that should prevent you from releasing and re-creating it in onStop and onStart; It just doesn't provide cleaner paths to automatically do it for you.

brianwernick avatar Apr 04 '17 14:04 brianwernick

I'm going to update this to a feature request and look in to adding that functionality. (most likely post 4.1.0 though)

brianwernick avatar Apr 04 '17 15:04 brianwernick

Sounds good, thanks!

saket avatar Apr 07 '17 07:04 saket

Is there any good solutions for preventing releasing when play in RecyclerView? I just found that the VideoView is released after item being scrolled up or down and unvisible for user (the item view was detached from window). Must i call setReleaseOnDetachFromWindow() for this problem? To be honest, i don't want to cache holders or VideoViews by myself when they are not used or visible. But if i don't do that, i can't find all of them to reease when exiting activity.

leimenghao avatar Aug 19 '17 02:08 leimenghao

Is there any good solutions for preventing releasing when play in RecyclerView? I just found that the VideoView is released after item being scrolled up or down and unvisible for user (the item view was detached from window). Must i call setReleaseOnDetachFromWindow() for this problem? To be honest, i don't want to cache holders or VideoViews by myself when they are not used or visible. But if i don't do that, i can't find all of them to reease when exiting activity.

Same problem, @brianwernick any suggestion to handle this in RecyclerView ?

ominfowave avatar Sep 13 '17 11:09 ominfowave

Guys, if have a different question create a new issue instead of trying to hijack the actual one.

brianwernick avatar Sep 14 '17 14:09 brianwernick

Issue resolved for me, just use onDestroy method to removeVideoApi and invokeStop

@Override protected void onStop() { super.onStop(); Log.d(VideoPlayerActivity.class.getCanonicalName(), "onStop: "); playlistManager.unRegisterPlaylistListener(this); if (videoApi.isPlaying()) { playlistManager.invokePausePlay(); } }

@Override protected void onDestroy() { playlistManager.removeVideoApi(videoApi); playlistManager.invokeStop(); super.onDestroy(); Log.d(VideoPlayerActivity.class.getCanonicalName(), "onDestroy: "); }

hamzaahmedkhan avatar Dec 24 '19 15:12 hamzaahmedkhan