Allow media to be played automatically (#1153)

* Allow media to be played automatically

* Fix for media been always autoplayed when opened from miniplayer
This commit is contained in:
Javi Rueda 2021-02-26 19:24:17 +01:00 committed by GitHub
parent b2f56364d6
commit f328efb831
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 6 deletions

View file

@ -336,6 +336,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
// preference keys // preference keys
public static final String PREFERENCE_KEY_BACKGROUND_PLAYBACK = "io.lbry.browser.preference.userinterface.BackgroundPlayback"; 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_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_MATURE_CONTENT = "io.lbry.browser.preference.userinterface.ShowMatureContent";
public static final String PREFERENCE_KEY_SHOW_URL_SUGGESTIONS = "io.lbry.browser.preference.userinterface.UrlSuggestions"; 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); 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() { public boolean initialSubscriptionMergeDone() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, false); return sp.getBoolean(PREFERENCE_KEY_INTERNAL_INITIAL_SUBSCRIPTION_MERGE_DONE, false);

View file

@ -12,7 +12,6 @@ import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.format.DateUtils; import android.text.format.DateUtils;
@ -98,6 +97,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -284,14 +284,13 @@ public class FileViewFragment extends BaseFragment implements
fileViewPlayerListener = new Player.EventListener() { fileViewPlayerListener = new Player.EventListener() {
@Override @Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { public void onPlaybackStateChanged(@Player.State int playbackState) {
if (playbackState == Player.STATE_READY) { if (playbackState == Player.STATE_READY) {
elapsedDuration = MainActivity.appPlayer.getCurrentPosition(); elapsedDuration = MainActivity.appPlayer.getCurrentPosition();
totalDuration = MainActivity.appPlayer.getDuration() < 0 ? 0 : MainActivity.appPlayer.getDuration(); totalDuration = MainActivity.appPlayer.getDuration() < 0 ? 0 : MainActivity.appPlayer.getDuration();
if (!playbackStarted) { if (!playbackStarted) {
logPlay(currentUrl, startTimeMillis); logPlay(currentUrl, startTimeMillis);
playbackStarted = true; playbackStarted = true;
isPlaying = true;
long lastPosition = loadLastPlaybackPosition(); long lastPosition = loadLastPlaybackPosition();
if (lastPosition > -1) { if (lastPosition > -1) {
@ -303,7 +302,7 @@ public class FileViewFragment extends BaseFragment implements
hideBuffering(); hideBuffering();
if (loadingNewClaim) { if (loadingNewClaim) {
MainActivity.appPlayer.setPlayWhenReady(true); MainActivity.appPlayer.setPlayWhenReady(Objects.requireNonNull((MainActivity) (getActivity())).isMediaAutoplayEnabled());
loadingNewClaim = false; loadingNewClaim = false;
} }
} else if (playbackState == Player.STATE_BUFFERING) { } else if (playbackState == Player.STATE_BUFFERING) {
@ -340,6 +339,11 @@ public class FileViewFragment extends BaseFragment implements
hideBuffering(); hideBuffering();
} }
} }
@Override
public void onIsPlayingChanged(boolean isPlayng) {
isPlaying = isPlayng;
}
}; };
return root; return root;
@ -1762,7 +1766,7 @@ public class FileViewFragment extends BaseFragment implements
((MainActivity) context).setNowPlayingClaim(claim, currentUrl); ((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 userAgent = Util.getUserAgent(context, getString(R.string.app_name));
String mediaSourceUrl = getStreamingUrl(); String mediaSourceUrl = getStreamingUrl();
MediaSource mediaSource = new ProgressiveMediaSource.Factory( MediaSource mediaSource = new ProgressiveMediaSource.Factory(
@ -1770,7 +1774,8 @@ public class FileViewFragment extends BaseFragment implements
new DefaultExtractorsFactory() new DefaultExtractorsFactory()
).setLoadErrorHandlingPolicy(new StreamLoadErrorPolicy()).createMediaSource(Uri.parse(mediaSourceUrl)); ).setLoadErrorHandlingPolicy(new StreamLoadErrorPolicy()).createMediaSource(Uri.parse(mediaSourceUrl));
MainActivity.appPlayer.prepare(mediaSource, true, true); MainActivity.appPlayer.setMediaSource(mediaSource, true);
MainActivity.appPlayer.prepare();
} }
} }
} }

View file

@ -247,6 +247,7 @@
<string name="user_interface">Content &amp; User interface</string> <string name="user_interface">Content &amp; User interface</string>
<string name="other">Other</string> <string name="other">Other</string>
<string name="enable_background_playback">Enable background playback</string> <string name="enable_background_playback">Enable background playback</string>
<string name="enable_autoplay">Autoplay media files</string>
<string name="enable_dark_mode">Enable dark theme</string> <string name="enable_dark_mode">Enable dark theme</string>
<string name="show_mature_content">Show mature content</string> <string name="show_mature_content">Show mature content</string>
<string name="show_url_suggestions">Show URL suggestions</string> <string name="show_url_suggestions">Show URL suggestions</string>

View file

@ -10,6 +10,11 @@
app:defaultValue="true" app:defaultValue="true"
app:title="@string/enable_background_playback" app:title="@string/enable_background_playback"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
app:key="io.lbry.browser.preference.userinterface.MediaAutoplay"
app:defaultValue="true"
app:title="@string/enable_autoplay"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat <SwitchPreferenceCompat
app:key="io.lbry.browser.preference.userinterface.DarkMode" app:key="io.lbry.browser.preference.userinterface.DarkMode"
app:title="@string/enable_dark_mode" app:title="@string/enable_dark_mode"