Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions app/src/main/java/org/schabi/newpipe/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import static com.google.android.exoplayer2.Player.RepeatMode;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveAutoVolumeBoostFromPrefs;
import static org.schabi.newpipe.player.helper.PlayerHelper.retrievePlaybackParametersFromPrefs;
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveSeekDurationFromPreferences;
import static org.schabi.newpipe.player.helper.PlayerHelper.retrieveVolumeBoostFromPrefs;
import static org.schabi.newpipe.player.helper.PlayerHelper.savePlaybackParametersToPrefs;
import static org.schabi.newpipe.player.helper.PlayerHelper.saveVolumeBoostToPrefs;
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE;
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD;
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND;
Expand Down Expand Up @@ -67,7 +70,6 @@
import androidx.preference.PreferenceManager;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.PlaybackParameters;
Expand Down Expand Up @@ -102,6 +104,7 @@
import org.schabi.newpipe.player.helper.LoadController;
import org.schabi.newpipe.player.helper.PlayerDataSource;
import org.schabi.newpipe.player.helper.PlayerHelper;
import org.schabi.newpipe.player.helper.VolumeBoostRenderersFactory;
import org.schabi.newpipe.player.mediaitem.MediaItemTag;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.player.notification.NotificationPlayerUi;
Expand Down Expand Up @@ -214,7 +217,7 @@ public final class Player implements PlaybackListener, Listener {
@NonNull
private final LoadController loadController;
@NonNull
private final DefaultRenderersFactory renderFactory;
private final VolumeBoostRenderersFactory renderFactory;

@NonNull
private final VideoPlaybackResolver videoResolver;
Expand Down Expand Up @@ -299,7 +302,8 @@ public Player(@NonNull final PlayerService service,
renderFactory = prefs.getBoolean(
context.getString(
R.string.always_use_exoplayer_set_output_surface_workaround_key), false)
? new CustomRenderersFactory(context) : new DefaultRenderersFactory(context);
? new CustomRenderersFactory(context)
: new VolumeBoostRenderersFactory(context);

renderFactory.setEnableDecoderFallback(
prefs.getBoolean(
Expand Down Expand Up @@ -607,6 +611,8 @@ private void initPlayback(@NonNull final PlayQueue queue,
R.string.playback_skip_silence_key), getPlaybackSkipSilence());
final PlaybackParameters savedParameters = retrievePlaybackParametersFromPrefs(this);
setPlaybackParameters(savedParameters.speed, savedParameters.pitch, playbackSkipSilence);
setVolumeBoost(retrieveVolumeBoostFromPrefs(this),
retrieveAutoVolumeBoostFromPrefs(this));

playQueue = queue;
playQueue.init();
Expand Down Expand Up @@ -966,6 +972,32 @@ public boolean getPlaybackSkipSilence() {
return !exoPlayerIsNull() && simpleExoPlayer.getSkipSilenceEnabled();
}

public float getVolumeBoost() {
return renderFactory.getVolumeBoost();
}

public boolean isAutoVolumeBoostEnabled() {
return renderFactory.isAutomaticVolumeBoost();
}

/**
* Sets how much the decoded audio should be amplified, and also saves it to shared
* preferences. The gain is rounded up to 2 decimal places before being used or saved.
*
* @param volumeBoost the gain to apply when {@code autoVolumeBoost} is disabled, where 1
* means no amplification at all
* @param autoVolumeBoost whether the gain should instead be computed in real time from the
* loudness of the stream being played
*/
public void setVolumeBoost(final float volumeBoost, final boolean autoVolumeBoost) {
final float roundedVolumeBoost = Math.round(volumeBoost * 100.0f) / 100.0f;

saveVolumeBoostToPrefs(this, roundedVolumeBoost, autoVolumeBoost);
renderFactory.setVolumeBoost(roundedVolumeBoost);
renderFactory.setAutomaticVolumeBoost(autoVolumeBoost);
UIs.call(PlayerUi::onVolumeBoostChanged);
}

public PlaybackParameters getPlaybackParameters() {
if (exoPlayerIsNull()) {
return PlaybackParameters.DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
import android.content.Context;
import android.os.Handler;

import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.Renderer;
import com.google.android.exoplayer2.mediacodec.MediaCodecSelector;
import com.google.android.exoplayer2.video.VideoRendererEventListener;

import java.util.ArrayList;

/**
* A {@link DefaultRenderersFactory} which only uses {@link CustomMediaCodecVideoRenderer} as an
* implementation of video codec renders.
* A {@link VolumeBoostRenderersFactory} which only uses {@link CustomMediaCodecVideoRenderer} as
* an implementation of video codec renders.
*
* <p>
* As no ExoPlayer extension is currently used, the reflection code used by ExoPlayer to try to
* load video extension libraries is not needed in our case and has been removed. This should be
* changed in the case an extension is shipped with the app, such as the AV1 one.
* </p>
*/
public final class CustomRenderersFactory extends DefaultRenderersFactory {
public final class CustomRenderersFactory extends VolumeBoostRenderersFactory {

public CustomRenderersFactory(final Context context) {
super(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ public static void savePlaybackParametersToPrefs(final Player player,
.apply();
}

public static float retrieveVolumeBoostFromPrefs(final Player player) {
return player.getPrefs().getFloat(player.getContext().getString(
R.string.playback_volume_boost_key), player.getVolumeBoost());
}

public static boolean retrieveAutoVolumeBoostFromPrefs(final Player player) {
return player.getPrefs().getBoolean(player.getContext().getString(
R.string.playback_auto_volume_boost_key), player.isAutoVolumeBoostEnabled());
}

public static void saveVolumeBoostToPrefs(final Player player,
final float volumeBoost,
final boolean autoVolumeBoost) {
player.getPrefs().edit()
.putFloat(player.getContext().getString(R.string.playback_volume_boost_key),
volumeBoost)
.putBoolean(player.getContext().getString(R.string.playback_auto_volume_boost_key),
autoVolumeBoost)
.apply();
}

public static float getMinimumVideoHeight(final float width) {
return width / (16.0f / 9.0f); // Respect the 16:9 ratio that most videos have
}
Expand Down
Loading
Loading