From f9a65d427793b59cc002bfd49d8248e91e118f95 Mon Sep 17 00:00:00 2001 From: Javi Rueda Date: Thu, 25 Feb 2021 17:27:16 +0100 Subject: [PATCH] Allow media to be played automatically --- app/src/main/java/io/lbry/browser/MainActivity.java | 6 ++++++ .../lbry/browser/ui/findcontent/FileViewFragment.java | 11 ++++++----- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings.xml | 5 +++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 522e1cdf..fcd34261 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -336,6 +336,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener // preference keys public static final String PREFERENCE_KEY_BACKGROUND_PLAYBACK = "io.lbry.browser.preference.userinterface.BackgroundPlayback"; + public static final String PREFERENCE_KEY_MEDIA_AUTOPLAY = "io.lbry.browser.preference.userinterface.MediaAutoplay"; public static final String PREFERENCE_KEY_DARK_MODE = "io.lbry.browser.preference.userinterface.DarkMode"; public static final String PREFERENCE_KEY_SHOW_MATURE_CONTENT = "io.lbry.browser.preference.userinterface.ShowMatureContent"; public static final String PREFERENCE_KEY_SHOW_URL_SUGGESTIONS = "io.lbry.browser.preference.userinterface.UrlSuggestions"; @@ -680,6 +681,11 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener return sp.getBoolean(PREFERENCE_KEY_BACKGROUND_PLAYBACK, true); } + public boolean isMediaAutoplayEnabled() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + return sp.getBoolean(PREFERENCE_KEY_MEDIA_AUTOPLAY, true); + } + public boolean initialSubscriptionMergeDone() { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); return sp.getBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, false); diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index e04e5068..ed8703dc 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -12,7 +12,6 @@ import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; import android.text.Editable; import android.text.TextWatcher; import android.text.format.DateUtils; @@ -98,6 +97,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -284,7 +284,7 @@ public class FileViewFragment extends BaseFragment implements fileViewPlayerListener = new Player.EventListener() { @Override - public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { + public void onPlaybackStateChanged(@Player.State int playbackState) { if (playbackState == Player.STATE_READY) { elapsedDuration = MainActivity.appPlayer.getCurrentPosition(); totalDuration = MainActivity.appPlayer.getDuration() < 0 ? 0 : MainActivity.appPlayer.getDuration(); @@ -303,7 +303,7 @@ public class FileViewFragment extends BaseFragment implements hideBuffering(); if (loadingNewClaim) { - MainActivity.appPlayer.setPlayWhenReady(true); + MainActivity.appPlayer.setPlayWhenReady(Objects.requireNonNull((MainActivity) (getActivity())).isMediaAutoplayEnabled()); loadingNewClaim = false; } } else if (playbackState == Player.STATE_BUFFERING) { @@ -1762,7 +1762,7 @@ public class FileViewFragment extends BaseFragment implements ((MainActivity) context).setNowPlayingClaim(claim, currentUrl); } - MainActivity.appPlayer.setPlayWhenReady(true); + MainActivity.appPlayer.setPlayWhenReady(Objects.requireNonNull((MainActivity) (getActivity())).isMediaAutoplayEnabled()); String userAgent = Util.getUserAgent(context, getString(R.string.app_name)); String mediaSourceUrl = getStreamingUrl(); MediaSource mediaSource = new ProgressiveMediaSource.Factory( @@ -1770,7 +1770,8 @@ public class FileViewFragment extends BaseFragment implements new DefaultExtractorsFactory() ).setLoadErrorHandlingPolicy(new StreamLoadErrorPolicy()).createMediaSource(Uri.parse(mediaSourceUrl)); - MainActivity.appPlayer.prepare(mediaSource, true, true); + MainActivity.appPlayer.setMediaSource(mediaSource, true); + MainActivity.appPlayer.prepare(); } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7463513e..5c4da9f9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -247,6 +247,7 @@ Content & User interface Other Enable background playback + Autoplay media files Enable dark theme Show mature content Show URL suggestions diff --git a/app/src/main/res/xml/settings.xml b/app/src/main/res/xml/settings.xml index 6348a2d9..9f588829 100644 --- a/app/src/main/res/xml/settings.xml +++ b/app/src/main/res/xml/settings.xml @@ -10,6 +10,11 @@ app:defaultValue="true" app:title="@string/enable_background_playback" app:iconSpaceReserved="false" /> +