Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mute button #3165

Merged
merged 15 commits into from Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
18 changes: 11 additions & 7 deletions app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
import android.graphics.Bitmap;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.source.MediaSource;
Expand Down Expand Up @@ -341,7 +341,6 @@ public void initPlayer(boolean playOnReady) {
@Override
public void handleIntent(final Intent intent) {
super.handleIntent(intent);

This conversation was marked as resolved.
Show resolved Hide resolved
resetNotification();
if (bigNotRemoteView != null)
bigNotRemoteView.setProgressBar(R.id.notificationProgressBar, 100, 0, false);
Expand Down Expand Up @@ -389,7 +388,6 @@ public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
@Override
public void onPrepared(boolean playWhenReady) {
super.onPrepared(playWhenReady);
simpleExoPlayer.setVolume(1f);
}

@Override
Expand All @@ -398,6 +396,12 @@ public void onShuffleClicked() {
updatePlayback();
}

@Override
public void onMuteUnmuteButtonClicked() {
super.onMuteUnmuteButtonClicked();
updatePlayback();
}

@Override
public void onUpdateProgress(int currentProgress, int duration, int bufferPercent) {
updateProgress(currentProgress, duration, bufferPercent);
Expand Down
26 changes: 22 additions & 4 deletions app/src/main/java/org/schabi/newpipe/player/BasePlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public abstract class BasePlayer implements
public static final String START_PAUSED = "start_paused";
@NonNull
public static final String SELECT_ON_APPEND = "select_on_append";
@NonNull
public static final String IS_MUTED = "is_muted";

/*//////////////////////////////////////////////////////////////////////////
// Playback
Expand Down Expand Up @@ -275,6 +277,7 @@ public void handleIntent(Intent intent) {
final float playbackPitch = intent.getFloatExtra(PLAYBACK_PITCH, getPlaybackPitch());
final boolean playbackSkipSilence = intent.getBooleanExtra(PLAYBACK_SKIP_SILENCE,
getPlaybackSkipSilence());
final boolean isMuted = intent.getBooleanExtra(IS_MUTED, isMuted());
This conversation was marked as resolved.
Show resolved Hide resolved

// seek to timestamp if stream is already playing
if (simpleExoPlayer != null
Expand All @@ -283,7 +286,7 @@ public void handleIntent(Intent intent) {
&& playQueue.getItem() != null
&& queue.getItem().getUrl().equals(playQueue.getItem().getUrl())
&& queue.getItem().getRecoveryPosition() != PlayQueueItem.RECOVERY_UNSET
) {
) {
simpleExoPlayer.seekTo(playQueue.getIndex(), queue.getItem().getRecoveryPosition());
return;

Expand All @@ -293,7 +296,7 @@ public void handleIntent(Intent intent) {
stateLoader = recordManager.loadStreamState(item)
.observeOn(AndroidSchedulers.mainThread())
.doFinally(() -> initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence,
/*playOnInit=*/true))
/*playOnInit=*/true, isMuted))
.subscribe(
state -> queue.setRecovery(queue.getIndex(), state.getProgressTime()),
error -> {
Expand All @@ -306,15 +309,16 @@ public void handleIntent(Intent intent) {
}
// Good to go...
initPlayback(queue, repeatMode, playbackSpeed, playbackPitch, playbackSkipSilence,
/*playOnInit=*/!intent.getBooleanExtra(START_PAUSED, false));
/*playOnInit=*/!intent.getBooleanExtra(START_PAUSED, false), isMuted);
}

protected void initPlayback(@NonNull final PlayQueue queue,
@Player.RepeatMode final int repeatMode,
final float playbackSpeed,
final float playbackPitch,
final boolean playbackSkipSilence,
final boolean playOnReady) {
final boolean playOnReady,
final boolean isMuted) {
destroyPlayer();
initPlayer(playOnReady);
setRepeatMode(repeatMode);
Expand All @@ -327,6 +331,8 @@ protected void initPlayback(@NonNull final PlayQueue queue,

if (playQueueAdapter != null) playQueueAdapter.dispose();
playQueueAdapter = new PlayQueueAdapter(context, playQueue);

if (isMuted) simpleExoPlayer.setVolume(0);
This conversation was marked as resolved.
Show resolved Hide resolved
}

public void destroyPlayer() {
Expand Down Expand Up @@ -532,6 +538,18 @@ public void onShuffleClicked() {
if (simpleExoPlayer == null) return;
simpleExoPlayer.setShuffleModeEnabled(!simpleExoPlayer.getShuffleModeEnabled());
}
/*//////////////////////////////////////////////////////////////////////////
// Mute / Unmute
//////////////////////////////////////////////////////////////////////////*/

public void onMuteUnmuteButtonClicked() {
if (DEBUG) Log.d(TAG, "onMuteUnmuteButtonClicled() called");
simpleExoPlayer.setVolume(isMuted() ? 1 : 0);
}

public boolean isMuted() {
return simpleExoPlayer.getVolume() == 0;
}

/*//////////////////////////////////////////////////////////////////////////
// Progress Updates
Expand Down
68 changes: 53 additions & 15 deletions app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.ItemTouchHelper;

import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
Expand Down Expand Up @@ -114,7 +117,8 @@ public final class MainVideoPlayer extends AppCompatActivity

private SharedPreferences defaultPreferences;

@Nullable private PlayerState playerState;
@Nullable
private PlayerState playerState;
private boolean isInMultiWindow;
private boolean isBackPressed;

Expand All @@ -128,11 +132,13 @@ public final class MainVideoPlayer extends AppCompatActivity
protected void onCreate(@Nullable Bundle savedInstanceState) {
assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState);
if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
if (DEBUG)
Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this);
ThemeHelper.setTheme(this);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) getWindow().setStatusBarColor(Color.BLACK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getWindow().setStatusBarColor(Color.BLACK);
setVolumeControlStream(AudioManager.STREAM_MUSIC);

WindowManager.LayoutParams lp = getWindow().getAttributes();
Expand All @@ -141,7 +147,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

hideSystemUi();
setContentView(R.layout.activity_main_player);
playerImpl = new VideoPlayerImpl(this);
playerImpl = new VideoPlayerImpl(this);
playerImpl.setup(findViewById(android.R.id.content));

if (savedInstanceState != null && savedInstanceState.get(KEY_SAVED_STATE) != null) {
Expand Down Expand Up @@ -218,7 +224,7 @@ protected void onResume() {
playerImpl.setPlaybackQuality(playerState.getPlaybackQuality());
playerImpl.initPlayback(playerState.getPlayQueue(), playerState.getRepeatMode(),
playerState.getPlaybackSpeed(), playerState.getPlaybackPitch(),
playerState.isPlaybackSkipSilence(), playerState.wasPlaying());
playerState.isPlaybackSkipSilence(), playerState.wasPlaying(), playerImpl.isMuted());
}
}

Expand Down Expand Up @@ -246,7 +252,7 @@ protected void onSaveInstanceState(Bundle outState) {
if (playerImpl == null) return;

playerImpl.setRecovery();
if(!playerImpl.gotDestroyed()) {
if (!playerImpl.gotDestroyed()) {
playerState = createPlayerState();
}
StateSaver.tryToSave(isChangingConfigurations(), null, outState, this);
Expand Down Expand Up @@ -394,6 +400,16 @@ protected void setShuffleButton(final ImageButton shuffleButton, final boolean s
shuffleButton.setImageAlpha(shuffleAlpha);
}

protected void setMuteButton(final ImageButton muteButton, final boolean isMuted) {
if (isMuted) {
muteButton.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.white));
} else {
muteButton.setColorFilter(ContextCompat.getColor(getApplicationContext(), R.color.gray));

}
This conversation was marked as resolved.
Show resolved Hide resolved
}


private boolean isInMultiWindow() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && isInMultiWindowMode();
}
Expand Down Expand Up @@ -446,6 +462,7 @@ private class VideoPlayerImpl extends VideoPlayer {
private ImageButton toggleOrientationButton;
private ImageButton switchPopupButton;
private ImageButton switchBackgroundButton;
private ImageButton muteButton;

private RelativeLayout windowRootLayout;
private View secondaryControls;
Expand Down Expand Up @@ -482,6 +499,7 @@ public void initViews(View rootView) {
this.shareButton = rootView.findViewById(R.id.share);
this.toggleOrientationButton = rootView.findViewById(R.id.toggleOrientation);
this.switchBackgroundButton = rootView.findViewById(R.id.switchBackground);
this.muteButton = rootView.findViewById(R.id.switchMute);
this.switchPopupButton = rootView.findViewById(R.id.switchPopup);

this.queueLayout = findViewById(R.id.playQueuePanel);
Expand All @@ -491,7 +509,7 @@ public void initViews(View rootView) {
titleTextView.setSelected(true);
channelTextView.setSelected(true);
boolean showKodiButton = PreferenceManager.getDefaultSharedPreferences(this.context).getBoolean(
this.context.getString(R.string.show_play_with_kodi_key), false);
this.context.getString(R.string.show_play_with_kodi_key), false);
kodiButton.setVisibility(showKodiButton ? View.VISIBLE : View.GONE);

getRootView().setKeepScreenOn(true);
Expand Down Expand Up @@ -533,6 +551,7 @@ public void initListeners() {
shareButton.setOnClickListener(this);
toggleOrientationButton.setOnClickListener(this);
switchBackgroundButton.setOnClickListener(this);
muteButton.setOnClickListener(this);
switchPopupButton.setOnClickListener(this);

getRootView().addOnLayoutChangeListener((view, l, t, r, b, ol, ot, or, ob) -> {
Expand Down Expand Up @@ -638,7 +657,8 @@ public void onFullScreenButtonClicked() {
this.getPlaybackSkipSilence(),
this.getPlaybackQuality(),
false,
!isPlaying()
!isPlaying(),
isMuted()
);
context.startService(intent);

Expand All @@ -662,7 +682,8 @@ public void onPlayBackgroundButtonClicked() {
this.getPlaybackSkipSilence(),
this.getPlaybackQuality(),
false,
!isPlaying()
!isPlaying(),
isMuted()
);
context.startService(intent);

Expand All @@ -671,6 +692,12 @@ public void onPlayBackgroundButtonClicked() {
finish();
}

@Override
public void onMuteUnmuteButtonClicked() {
super.onMuteUnmuteButtonClicked();
setMuteButton(muteButton, playerImpl.isMuted());
}


@Override
public void onClick(View v) {
Expand Down Expand Up @@ -708,11 +735,14 @@ public void onClick(View v) {
} else if (v.getId() == switchBackgroundButton.getId()) {
onPlayBackgroundButtonClicked();

} else if (v.getId() == muteButton.getId()) {
onMuteUnmuteButtonClicked();

} else if (v.getId() == closeButton.getId()) {
onPlaybackShutdown();
return;
} else if (v.getId() == kodiButton.getId()) {
onKodiShare();
onKodiShare();
}

if (getCurrentState() != STATE_COMPLETED) {
Expand Down Expand Up @@ -755,13 +785,14 @@ private void onMoreOptionsClicked() {
animateView(secondaryControls, SLIDE_AND_ALPHA, !isMoreControlsVisible,
DEFAULT_CONTROLS_DURATION);
showControls(DEFAULT_CONTROLS_DURATION);
setMuteButton(muteButton, playerImpl.isMuted());
}

private void onShareClicked() {
// share video at the current time (youtube.com/watch?v=ID&t=SECONDS)
ShareUtils.shareUrl(MainVideoPlayer.this,
playerImpl.getVideoTitle(),
playerImpl.getVideoUrl() + "&t=" + String.valueOf(playerImpl.getPlaybackSeekBar().getProgress()/1000));
playerImpl.getVideoUrl() + "&t=" + String.valueOf(playerImpl.getPlaybackSeekBar().getProgress() / 1000));
}

private void onScreenRotationClicked() {
Expand Down Expand Up @@ -994,7 +1025,7 @@ public void onMove(int sourceIndex, int targetIndex) {

@Override
public void onSwiped(int index) {
if(index != -1) playQueue.remove(index);
if (index != -1) playQueue.remove(index);
}
};
}
Expand Down Expand Up @@ -1059,6 +1090,10 @@ public ImageButton getRepeatButton() {
return repeatButton;
}

public ImageButton getMuteButton() {
return muteButton;
}

public ImageButton getPlayPauseButton() {
return playPauseButton;
}
Expand All @@ -1073,7 +1108,8 @@ private class PlayerGestureListener extends GestureDetector.SimpleOnGestureListe

@Override
public boolean onDoubleTap(MotionEvent e) {
if (DEBUG) Log.d(TAG, "onDoubleTap() called with: e = [" + e + "]" + "rawXy = " + e.getRawX() + ", " + e.getRawY() + ", xy = " + e.getX() + ", " + e.getY());
if (DEBUG)
Log.d(TAG, "onDoubleTap() called with: e = [" + e + "]" + "rawXy = " + e.getRawX() + ", " + e.getRawY() + ", xy = " + e.getX() + ", " + e.getY());

if (e.getX() > playerImpl.getRootView().getWidth() * 2 / 3) {
playerImpl.onFastForward();
Expand Down Expand Up @@ -1169,7 +1205,8 @@ public boolean onScroll(MotionEvent initialEvent, MotionEvent movingEvent, float
layoutParams.screenBrightness = currentProgressPercent;
getWindow().setAttributes(layoutParams);

if (DEBUG) Log.d(TAG, "onScroll().brightnessControl, currentBrightness = " + currentProgressPercent);
if (DEBUG)
Log.d(TAG, "onScroll().brightnessControl, currentBrightness = " + currentProgressPercent);

final int resId =
currentProgressPercent < 0.25 ? R.drawable.ic_brightness_low_white_72dp
Expand Down Expand Up @@ -1208,7 +1245,8 @@ private void onScrollEnd() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//noinspection PointlessBooleanExpression
if (DEBUG && false) Log.d(TAG, "onTouch() called with: v = [" + v + "], event = [" + event + "]");
if (DEBUG && false)
Log.d(TAG, "onTouch() called with: v = [" + v + "], event = [" + event + "]");
gestureDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP && isMoving) {
isMoving = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ public void onFullScreenButtonClicked() {
this.getPlaybackSkipSilence(),
this.getPlaybackQuality(),
false,
!isPlaying()
!isPlaying(),
isMuted()
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Expand Down Expand Up @@ -607,6 +608,12 @@ public void onShuffleClicked() {
updatePlayback();
}

@Override
public void onMuteUnmuteButtonClicked() {
super.onMuteUnmuteButtonClicked();
updatePlayback();
}

@Override
public void onUpdateProgress(int currentProgress, int duration, int bufferPercent) {
updateProgress(currentProgress, duration, bufferPercent);
Expand Down
Loading