public void togglePlayback(boolean playPause) { if (playPause) { startProgressAutomation(); setFadingEnabled(true); mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), mPlaybackControlsRow.getCurrentTime(), true); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PAUSE)); } else { stopProgressAutomation(); setFadingEnabled(false); mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), mPlaybackControlsRow.getCurrentTime(), false); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PLAY)); } notifyChanged(mPlayPauseAction); }
@Override public void onActionClicked(@NonNull Action action) { if (action.getId() == mPlayPauseAction.getId()) { invokeTogglePlaybackAction(mPlayPauseAction.getIndex() == PlayPauseAction.PLAY); } else if (action.getId() == mScaleVideoAction.getId()) { invokeScaleVideoAction(); } else if (action.getId() == mClosedCaptioningAction.getId()) { invokeOpenSubtitleSettingsAction(); } if (mSkipPreviousAction != null && action.getId() == mSkipPreviousAction.getId()) { playSelectedEpisode(mPreviousEpisode); } if (mSkipNextAction != null && action.getId() == mSkipNextAction.getId()) { playSelectedEpisode(mNextEpisode); } if (action instanceof PlaybackControlsRow.MultiAction) { notifyPlaybackControlActionChanged(action); } }
private void setupPlaybackControlItemsToInitialisingState() { mRowsAdapter.clear(); mPlaybackControlsRow = new PlaybackControlsRow(mStreamInfo); mPlaybackControlsRow.setCurrentTime(0); mPlaybackControlsRow.setBufferedProgress(0); ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector(); mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); Activity activity = getActivity(); mPlayPauseAction = new PlayPauseAction(activity); mPrimaryActionsAdapter.add(mPlayPauseAction); setupSecondaryRowPlaybackControl(presenterSelector); mRowsAdapter.add(mPlaybackControlsRow); mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size()); }
private void setupPrimaryRowPlaybackControl(@NonNull ControlButtonPresenterSelector presenterSelector) { mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); Activity activity = getActivity(); mPlayPauseAction = new PlayPauseAction(activity); mFastForwardAction = new FastForwardAction(activity); mRewindAction = new RewindAction(activity); if (mPreviousEpisode != null) { mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(activity); mPrimaryActionsAdapter.add(mSkipPreviousAction); } // Add main controls to primary adapter. mPrimaryActionsAdapter.add(mRewindAction); mPrimaryActionsAdapter.add(mPlayPauseAction); mPrimaryActionsAdapter.add(mFastForwardAction); if (mNextEpisode != null) { mSkipNextAction = new PlaybackControlsRow.SkipNextAction(activity); mPrimaryActionsAdapter.add(mSkipNextAction); } }
private void addPlaybackControlsRow(MediaMetadata metadata) { mPlaybackControlsRow = new PlaybackControlsRow(new MutableMediaMetadataHolder(metadata)); mRowsAdapter.add(mPlaybackControlsRow); resetPlaybackRow(); ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector(); mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); mPlayPauseAction = new PlayPauseAction(getActivity()); mSkipNextAction = new PlaybackControlsRow.SkipNextAction(getActivity()); mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(getActivity()); mPrimaryActionsAdapter.add(mSkipPreviousAction); mPrimaryActionsAdapter.add(mPlayPauseAction); mPrimaryActionsAdapter.add(mSkipNextAction); }
protected void updatePlaybackState(PlaybackState state) { if (mPlaybackControlsRow == null) { // We only update playback state after we get a valid metadata. return; } mLastPosition = state.getPosition(); mLastPositionUpdateTime = state.getLastPositionUpdateTime(); switch (state.getState()) { case PlaybackState.STATE_PLAYING: startProgressAutomation(); mPlayPauseAction.setIndex(PlayPauseAction.PAUSE); break; case PlaybackState.STATE_PAUSED: stopProgressAutomation(); mPlayPauseAction.setIndex(PlayPauseAction.PLAY); break; } updatePlayListRow(getActivity().getMediaController().getQueue()); mRowsAdapter.notifyArrayItemRangeChanged( mRowsAdapter.indexOf(mPlaybackControlsRow), 1); }
private void addPlaybackControlsRow(MediaMetadataCompat metadata) { mPlaybackControlsRow = new PlaybackControlsRow(new MutableMediaMetadataHolder(metadata)); mRowsAdapter.add(mPlaybackControlsRow); resetPlaybackRow(); ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector(); mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); mPlayPauseAction = new PlayPauseAction(getActivity()); mSkipNextAction = new PlaybackControlsRow.SkipNextAction(getActivity()); mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(getActivity()); mPrimaryActionsAdapter.add(mSkipPreviousAction); mPrimaryActionsAdapter.add(mPlayPauseAction); mPrimaryActionsAdapter.add(mSkipNextAction); }
protected void updatePlaybackState(PlaybackStateCompat state) { if (mPlaybackControlsRow == null) { // We only update playback state after we get a valid metadata. return; } mLastPosition = state.getPosition(); mLastPositionUpdateTime = state.getLastPositionUpdateTime(); switch (state.getState()) { case PlaybackStateCompat.STATE_PLAYING: startProgressAutomation(); mPlayPauseAction.setIndex(PlayPauseAction.PAUSE); break; case PlaybackStateCompat.STATE_PAUSED: stopProgressAutomation(); mPlayPauseAction.setIndex(PlayPauseAction.PLAY); break; } MediaControllerCompat controller = MediaControllerCompat.getMediaController(getActivity()); updatePlayListRow(controller.getQueue()); mRowsAdapter.notifyArrayItemRangeChanged( mRowsAdapter.indexOf(mPlaybackControlsRow), 1); }
@Override public void onPlaybackStateChanged(PlaybackState state) { Log.d(TAG, "playback state changed: " + state.getState()); if (state.getState() == PlaybackState.STATE_PLAYING && mCurrentPlaybackState != PlaybackState.STATE_PLAYING) { mCurrentPlaybackState = PlaybackState.STATE_PLAYING; startProgressAutomation(); setFadingEnabled(true); mPlayPauseAction.setIndex(PlayPauseAction.PAUSE); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PAUSE)); notifyChanged(mPlayPauseAction); } else if (state.getState() == PlaybackState.STATE_PAUSED && mCurrentPlaybackState != PlaybackState.STATE_PAUSED) { mCurrentPlaybackState = PlaybackState.STATE_PAUSED; stopProgressAutomation(); setFadingEnabled(false); mPlayPauseAction.setIndex(PlayPauseAction.PLAY); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PLAY)); notifyChanged(mPlayPauseAction); } int currentTime = (int)state.getPosition(); mPlaybackControlsRow.setCurrentTime(currentTime); mPlaybackControlsRow.setBufferedProgress(currentTime + SIMULATED_BUFFERED_TIME); }
private void next() { if (++mCurrentItem >= mItems.size()) { mCurrentItem = 0; } if (mPlayPauseAction.getIndex() == PlayPauseAction.PLAY) { mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, false); } else { mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, true); } updatePlaybackRow(mCurrentItem); }
private void prev() { if (--mCurrentItem < 0) { mCurrentItem = mItems.size() - 1; } if (mPlayPauseAction.getIndex() == PlayPauseAction.PLAY) { mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, false); } else { mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, true); } updatePlaybackRow(mCurrentItem); }
@Override public void onPause() { super.onPause(); if (!mKeepEventBusRegistration) { // Event is unregistered before playback paused event is sent mPlayPauseAction.setIndex(PlayPauseAction.PLAY); setFadingEnabled(false); notifyPlaybackControlActionChanged(mPlayPauseAction); EventBus.getDefault().unregister(this); } }
@Override public void onPlaybackStateChanged(@NonNull PlaybackState state) { if (state.getState() == PlaybackState.STATE_PLAYING && mCurrentPlaybackState == PlaybackState.STATE_PLAYING) { startProgressAutomation(); setFadingEnabled(true); } else if (state.getState() == PlaybackState.STATE_PLAYING) { mCurrentPlaybackState = PlaybackState.STATE_PLAYING; startProgressAutomation(); setFadingEnabled(true); mPlayPauseAction.setIndex(PlayPauseAction.PAUSE); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PAUSE)); notifyChanged(mPlayPauseAction); } else if (state.getState() == PlaybackState.STATE_PAUSED && mCurrentPlaybackState != PlaybackState.STATE_PAUSED) { mCurrentPlaybackState = PlaybackState.STATE_PAUSED; stopProgressAutomation(); setFadingEnabled(false); mPlayPauseAction.setIndex(PlayPauseAction.PLAY); mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PLAY)); notifyChanged(mPlayPauseAction); } int currentTime; if (state.getState() == PlaybackState.STATE_PAUSED || state.getState() == PlaybackState.STATE_PLAYING) { currentTime = mPlaybackControlsRow.getCurrentTime(); } else { currentTime = (int) state.getPosition(); } mPlaybackControlsRow.setCurrentTime(currentTime); mPlaybackControlsRow.setBufferedProgress(currentTime + SIMULATED_BUFFERED_TIME); }