Fix video player state after app resumption

This commit is contained in:
Grishka 2023-11-02 02:21:11 +03:00
parent 1d5b84943d
commit 09d0e82216
1 changed files with 18 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import android.graphics.Insets;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.SurfaceTexture; import android.graphics.SurfaceTexture;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.AudioManager; import android.media.AudioManager;
@ -533,23 +534,31 @@ public class PhotoViewer implements ZoomPanView.Listener{
} }
} }
private MediaPlayer findCurrentVideoPlayer(){ private GifVViewHolder findCurrentVideoPlayerHolder(){
RecyclerView rv=(RecyclerView) pager.getChildAt(0); RecyclerView rv=(RecyclerView) pager.getChildAt(0);
if(rv.findViewHolderForAdapterPosition(pager.getCurrentItem()) instanceof GifVViewHolder vvh && vvh.playerReady){ if(rv.findViewHolderForAdapterPosition(pager.getCurrentItem()) instanceof GifVViewHolder vvh && vvh.playerReady){
return vvh.player; return vvh;
} }
return null; return null;
} }
private MediaPlayer findCurrentVideoPlayer(){
GifVViewHolder holder=findCurrentVideoPlayerHolder();
return holder!=null ? holder.player : null;
}
private void pauseVideo(){ private void pauseVideo(){
MediaPlayer player=findCurrentVideoPlayer(); GifVViewHolder holder=findCurrentVideoPlayerHolder();
if(player==null || !player.isPlaying()) if(holder==null || !holder.player.isPlaying())
return; return;
player.pause(); holder.player.pause();
videoPlayPauseButton.setImageResource(R.drawable.ic_play_24); videoPlayPauseButton.setImageResource(R.drawable.ic_play_24);
videoPlayPauseButton.setContentDescription(activity.getString(R.string.play)); videoPlayPauseButton.setContentDescription(activity.getString(R.string.play));
stopUpdatingVideoPosition(); stopUpdatingVideoPosition();
windowView.removeCallbacks(uiAutoHider); windowView.removeCallbacks(uiAutoHider);
// Some MediaPlayer implementations clear the texture when the app goes into background.
// This makes sure the frame on which the video was paused is retained on the screen.
holder.wrap.setBackground(new BitmapDrawable(holder.textureView.getBitmap()));
} }
private void resumeVideo(){ private void resumeVideo(){
@ -817,7 +826,10 @@ public class PhotoViewer implements ZoomPanView.Listener{
@Override @Override
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface){ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface){
// A new frame of video was rendered. Clear the thumbnail or paused frame, if any, to avoid overdraw and free up some memory.
if(player.isPlaying() && wrap.getBackground()!=null){
wrap.setBackground(null);
}
} }
private void startPlayer(){ private void startPlayer(){