From f328efb831262a1191da1b6a839643203b0fbdf1 Mon Sep 17 00:00:00 2001 From: Javi Rueda Date: Fri, 26 Feb 2021 19:24:17 +0100 Subject: [PATCH] Allow media to be played automatically (#1153) * Allow media to be played automatically * Fix for media been always autoplayed when opened from miniplayer --- .../main/java/io/lbry/browser/MainActivity.java | 6 ++++++ .../ui/findcontent/FileViewFragment.java | 17 +++++++++++------ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings.xml | 5 +++++ 4 files changed, 23 insertions(+), 6 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..b2248c62 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,14 +284,13 @@ 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(); if (!playbackStarted) { logPlay(currentUrl, startTimeMillis); playbackStarted = true; - isPlaying = true; long lastPosition = loadLastPlaybackPosition(); if (lastPosition > -1) { @@ -303,7 +302,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) { @@ -340,6 +339,11 @@ public class FileViewFragment extends BaseFragment implements hideBuffering(); } } + + @Override + public void onIsPlayingChanged(boolean isPlayng) { + isPlaying = isPlayng; + } }; return root; @@ -1762,7 +1766,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 +1774,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" /> +