1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-12-23 13:26:59 +00:00

Re-fix play/stop autoplaying videos when navigating away

This commit is contained in:
Ammar Githam 2020-08-25 00:38:27 +09:00
parent 3c94e827d3
commit 0608882127
3 changed files with 32 additions and 18 deletions

View File

@ -39,7 +39,6 @@ import com.facebook.datasource.BaseDataSubscriber;
import com.facebook.datasource.DataSource; import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.request.ImageRequest; import com.facebook.imagepipeline.request.ImageRequest;
import com.google.android.exoplayer2.SimpleExoPlayer;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
@ -107,6 +106,7 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = "MainHelper"; private static final String TAG = "MainHelper";
private static final double MAX_VIDEO_HEIGHT = 0.9 * Utils.displayMetrics.heightPixels; private static final double MAX_VIDEO_HEIGHT = 0.9 * Utils.displayMetrics.heightPixels;
private static final int RESIZED_VIDEO_HEIGHT = (int) (0.8 * Utils.displayMetrics.heightPixels); private static final int RESIZED_VIDEO_HEIGHT = (int) (0.8 * Utils.displayMetrics.heightPixels);
public static final boolean SHOULD_AUTO_PLAY = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS);
private static AsyncTask<?, ?, ?> currentlyExecuting; private static AsyncTask<?, ?, ?> currentlyExecuting;
private AsyncTask<Void, Void, FeedStoryModel[]> prevStoriesFetcher; private AsyncTask<Void, Void, FeedStoryModel[]> prevStoriesFetcher;
@ -279,7 +279,7 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
if (result != null) { if (result != null) {
topicIds = result.getIds(); topicIds = result.getIds();
rankToken = result.getToken(); rankToken = result.getToken();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>( ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(
mainActivity, android.R.layout.simple_spinner_dropdown_item, result.getNames()); mainActivity, android.R.layout.simple_spinner_dropdown_item, result.getNames());
mainActivity.mainBinding.discoverType.setAdapter(spinnerArrayAdapter); mainActivity.mainBinding.discoverType.setAdapter(spinnerArrayAdapter);
} }
@ -340,10 +340,10 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
private FeedAdapter feedAdapter; private FeedAdapter feedAdapter;
private RecyclerLazyLoader feedLazyLoader, discoverLazyLoader; private RecyclerLazyLoader feedLazyLoader, discoverLazyLoader;
private DiscoverAdapter discoverAdapter; private DiscoverAdapter discoverAdapter;
public SimpleExoPlayer currentFeedPlayer; // hack for remix drawer layout
private String cookie = settingsHelper.getString(Constants.COOKIE); private String cookie = settingsHelper.getString(Constants.COOKIE);
private boolean isLoggedIn; private boolean isLoggedIn;
private RequestManager glide; private RequestManager glide;
private VideoAwareRecyclerScroller videoAwareRecyclerScroller;
public MainHelper(@NonNull final MainActivity mainActivity) { public MainHelper(@NonNull final MainActivity mainActivity) {
stopCurrentExecutor(); stopCurrentExecutor();
@ -443,9 +443,10 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
@Override @Override
public void onDrawerOpened(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) { public void onDrawerOpened(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) {
if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) { if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) {
if (currentFeedPlayer != null) { if (videoAwareRecyclerScroller != null) {
final boolean shouldAutoplay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); if (SHOULD_AUTO_PLAY) {
currentFeedPlayer.setPlayWhenReady(shouldAutoplay); videoAwareRecyclerScroller.startPlaying();
}
} }
} else { } else {
// clear selection // clear selection
@ -456,8 +457,8 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
@Override @Override
public void onDrawerClosed(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) { public void onDrawerClosed(@NonNull final View drawerView, @MouseDrawer.EdgeGravity final int gravity) {
if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) { if (gravity == GravityCompat.START || drawerView == mainActivity.mainBinding.feedView.feedLayout) {
if (currentFeedPlayer != null) { if (videoAwareRecyclerScroller != null) {
currentFeedPlayer.setPlayWhenReady(false); videoAwareRecyclerScroller.stopPlaying();
} }
} else { } else {
// clear selection // clear selection
@ -674,9 +675,9 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
} }
})); }));
final boolean shouldAutoPlay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); if (SHOULD_AUTO_PLAY) {
if (shouldAutoPlay) { videoAwareRecyclerScroller = new VideoAwareRecyclerScroller();
mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(new VideoAwareRecyclerScroller()); mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(videoAwareRecyclerScroller);
} }
mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(new PauseGlideOnFlingScrollListener(glide)); mainActivity.mainBinding.feedView.feedPosts.addOnScrollListener(new PauseGlideOnFlingScrollListener(glide));
@ -1338,15 +1339,14 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
} }
public void onPause() { public void onPause() {
if (currentFeedPlayer != null) { if (videoAwareRecyclerScroller != null) {
currentFeedPlayer.setPlayWhenReady(false); videoAwareRecyclerScroller.stopPlaying();
} }
} }
public void onResume() { public void onResume() {
if (currentFeedPlayer != null) { if (videoAwareRecyclerScroller != null && SHOULD_AUTO_PLAY) {
final boolean shouldAutoplay = settingsHelper.getBoolean(Constants.AUTOPLAY_VIDEOS); videoAwareRecyclerScroller.startPlaying();
currentFeedPlayer.setPlayWhenReady(shouldAutoplay);
} }
} }

View File

@ -551,8 +551,8 @@ public final class MainActivity extends BaseLanguageActivity {
finish(); finish();
else if (requestCode == 6007) else if (requestCode == 6007)
Utils.showImportExportDialog(this); Utils.showImportExportDialog(this);
else if (requestCode == 6969 && mainHelper.currentFeedPlayer != null) // else if (requestCode == 6969 && mainHelper.currentFeedPlayer != null)
mainHelper.currentFeedPlayer.setPlayWhenReady(true); // mainHelper.currentFeedPlayer.setPlayWhenReady(true);
} }
@Override @Override

View File

@ -216,6 +216,20 @@ public class VideoAwareRecyclerScroller extends RecyclerView.OnScrollListener {
return null; return null;
} }
public void startPlaying() {
if (currentlyPlayingViewHolder == null) {
return;
}
currentlyPlayingViewHolder.startPlaying();
}
public void stopPlaying() {
if (currentlyPlayingViewHolder == null) {
return;
}
currentlyPlayingViewHolder.stopPlaying();
}
// private synchronized void attachVideo(final int itemPos, final RecyclerView recyclerView, final View itemView) { // private synchronized void attachVideo(final int itemPos, final RecyclerView recyclerView, final View itemView) {
// synchronized (LOCK) { // synchronized (LOCK) {
// if (recyclerView != null) { // if (recyclerView != null) {