implement skipping forward

This commit is contained in:
Felix 2017-04-04 01:28:14 +02:00
parent 0f285db904
commit 2c1ac1096f
5 changed files with 120 additions and 87 deletions

View file

@ -20,13 +20,19 @@ import android.support.v17.leanback.widget.OnActionClickedListener;
import android.support.v17.leanback.widget.PlaybackControlsRow;
import android.support.v17.leanback.widget.PlaybackControlsRowPresenter;
import android.support.v17.leanback.widget.SparseArrayObjectAdapter;
import android.util.Log;
import com.bumptech.glide.Glide;
import de.nicidienase.chaosflix.activities.ExoPlayerActivity;
import de.nicidienase.chaosflix.entities.recording.Event;
import de.nicidienase.chaosflix.entities.recording.Recording;
import de.nicidienase.chaosflix.entities.streaming.Room;
import de.nicidienase.chaosflix.entities.streaming.StreamUrl;
import de.nicidienase.chaosflix.fragments.ExoOverlayFragment;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
/**
* Created by felix on 26.03.17.
@ -38,7 +44,8 @@ public class PlaybackHelper extends PlaybackControlGlue {
private static final int DEFAULT_UPDATE_PERIOD = 500;
private static final int UPDATE_PERIOD = 16;
private static final String TAG = PlaybackHelper.class.getSimpleName();
private ExoOverlayFragment.PlaybackControlListener controlListener;
private Disposable thumbDisposable;
private ExoOverlayFragment.PlaybackControlListener mControlListener;
private BitmapDrawable mDrawable = null;
private Room room;
private StreamUrl stream;
@ -61,32 +68,29 @@ public class PlaybackHelper extends PlaybackControlGlue {
public PlaybackHelper(Context context, ExoOverlayFragment fragment, Event event, Recording recording){
super(context,SEEK_SPEEDS);
controlListener = (ExoOverlayFragment.PlaybackControlListener) context;
mControlListener = (ExoOverlayFragment.PlaybackControlListener) context;
this.fragment = fragment;
this.event = event;
this.recording = recording;
thumbDisposable = Observable.fromCallable(() ->
new BitmapDrawable(
fragment.getResources(),
Glide.with(getContext())
.load(event.getThumbUrl())
.asBitmap()
.into(-1, -1)
.get()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(Throwable::printStackTrace)
.subscribe(bitmapDrawable -> mDrawable = bitmapDrawable);
setup();
// if(event != null){
// Observable.fromCallable(() ->
// new BitmapDrawable(
// mContext.getResources(),
// Glide.with(getContext())
// .load(event.getThumbUrl())
// .asBitmap()
// .into(-1, -1)
// .get()))
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .doOnError(Throwable::printStackTrace)
// .subscribe(bitmapDrawable -> mDrawable = bitmapDrawable);
// }
}
public PlaybackHelper(Context context, ExoOverlayFragment fragment, Room room, StreamUrl stream ){
super(context, SEEK_SPEEDS);
controlListener = (ExoOverlayFragment.PlaybackControlListener) context;
mControlListener = (ExoOverlayFragment.PlaybackControlListener) context;
this.fragment = fragment;
this.room = room;
this.stream = stream;
@ -94,24 +98,6 @@ public class PlaybackHelper extends PlaybackControlGlue {
setup();
}
// @Override
// public PlaybackControlsRowPresenter createControlsRowAndPresenter() {
// PlaybackControlsRowPresenter presenter = super.createControlsRowAndPresenter();
// SparseArrayObjectAdapter primaryActionsAdapter = (SparseArrayObjectAdapter) getControlsRow().getPrimaryActionsAdapter();
//
// ArrayObjectAdapter adapter = new ArrayObjectAdapter(new ControlButtonPresenterSelector());
// getControlsRow().setSecondaryActionsAdapter(adapter);
//
// mPlayPauseAction = (PlaybackControlsRow.PlayPauseAction) primaryActionsAdapter.lookup(ACTION_PLAY_PAUSE);
// mFastForwardAction = (PlaybackControlsRow.FastForwardAction) primaryActionsAdapter.lookup(ACTION_FAST_FORWARD);
// mRewindAction = (PlaybackControlsRow.RewindAction) primaryActionsAdapter.lookup(ACTION_REWIND);
// mSkipNextAction = (PlaybackControlsRow.SkipNextAction) primaryActionsAdapter.lookup(ACTION_SKIP_TO_NEXT);
// mSkipPreviousAction = (PlaybackControlsRow.SkipPreviousAction) primaryActionsAdapter.lookup(ACTION_SKIP_TO_PREVIOUS);
//
//// presenter.setOnActionClickedListener(action -> dispatch(action));
// return presenter;
// }
private void setup() {
mMediaController = fragment.getActivity().getMediaController();
mTransportControls = mMediaController.getTransportControls();
@ -123,6 +109,12 @@ public class PlaybackHelper extends PlaybackControlGlue {
adapter = new ArrayObjectAdapter(new ControlButtonPresenterSelector());
getControlsRow().setSecondaryActionsAdapter(adapter);
mFastForwardAction = (PlaybackControlsRow.FastForwardAction) getPrimaryActionsAdapter()
.lookup(ACTION_FAST_FORWARD);
mRewindAction = (PlaybackControlsRow.RewindAction) getPrimaryActionsAdapter()
.lookup(ACTION_REWIND);
presenter.setOnActionClickedListener(new OnActionClickedListener() {
@Override
public void onActionClicked(Action action) {
@ -139,7 +131,13 @@ public class PlaybackHelper extends PlaybackControlGlue {
multiAction.nextIndex();
notifyActionChanged(multiAction);
}
super.onActionClicked(action);
if (action == mFastForwardAction) {
mTransportControls.fastForward();
} else if (action == mRewindAction) {
mTransportControls.rewind();
} else {
super.onActionClicked(action);
}
}
private void notifyActionChanged(PlaybackControlsRow.MultiAction multiAction) {
@ -170,10 +168,10 @@ public class PlaybackHelper extends PlaybackControlGlue {
@Override
public void run() {
int totalTime = getControlsRow().getTotalTime();
long currentPosition = controlListener.getCurrentPosition();
long currentPosition = mControlListener.getCurrentPosition();
getControlsRow().setCurrentTimeLong(currentPosition);
long bufferedPosition = controlListener.getBufferedPosition();
long bufferedPosition = mControlListener.getBufferedPosition();
getControlsRow().setBufferedProgressLong(bufferedPosition);
if (totalTime > 0 && totalTime <= currentPosition) {
@ -194,7 +192,7 @@ public class PlaybackHelper extends PlaybackControlGlue {
@Override
public boolean isMediaPlaying() {
return controlListener.isMediaPlaying();
return mControlListener.isMediaPlaying();
}
@Override
@ -237,14 +235,35 @@ public class PlaybackHelper extends PlaybackControlGlue {
if(getCurrentSpeedId() == speed){
return;
}
mTransportControls.play();
setFadingEnabled(true);
fragment.setFadingEnabled(true);
if(mTransportControls != null){
mTransportControls.play();
} else {
mControlListener.play();
}
}
@Override
protected void pausePlayback() {
mTransportControls.pause();
setFadingEnabled(false);
fragment.setFadingEnabled(false);
if(mTransportControls != null){
mTransportControls.pause();
} else {
mControlListener.pause();
}
}
@Override
protected void skipToNext() {
mTransportControls.skipToNext();
}
@Override
protected void skipToPrevious() {
mTransportControls.skipToPrevious();
}
private void stopProgressAnimation() {
if (mHandler != null && mUpdateProgressRunnable != null) {
@ -255,7 +274,8 @@ public class PlaybackHelper extends PlaybackControlGlue {
@Override
public long getSupportedActions() {
return ACTION_PLAY_PAUSE | ACTION_FAST_FORWARD | ACTION_REWIND;
return ACTION_PLAY_PAUSE | ACTION_FAST_FORWARD | ACTION_REWIND | ACTION_SKIP_TO_PREVIOUS |
ACTION_SKIP_TO_NEXT;
}
@Override
@ -265,11 +285,11 @@ public class PlaybackHelper extends PlaybackControlGlue {
@Override
public int getCurrentPosition() {
return (int) controlListener.getCurrentPosition();
return (int) mControlListener.getCurrentPosition();
}
public long getCurrentPositionLong(){
return controlListener.getCurrentPosition();
return mControlListener.getCurrentPosition();
}
private boolean mediaIsStream() {
@ -307,4 +327,10 @@ public class PlaybackHelper extends PlaybackControlGlue {
PlaybackHelper.this.adapter.notifyArrayItemRangeChanged(0,1);
}
}
public void onStop(){
if(thumbDisposable != null){
thumbDisposable.dispose();
}
}
}

View file

@ -11,7 +11,6 @@ import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import de.nicidienase.chaosflix.network.MediaApiService;
import io.reactivex.Observable;
import io.reactivex.Single;
/**

View file

@ -116,7 +116,6 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty
}
}
MediaSource mediaSource = buildMediaSource(Uri.parse(source), "");
player.setPlayWhenReady(true);
player.prepare(mediaSource);
}

View file

@ -1,5 +1,6 @@
package de.nicidienase.chaosflix.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
@ -8,19 +9,14 @@ import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.support.v17.leanback.app.PlaybackFragment;
import android.support.v17.leanback.widget.Action;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.ClassPresenterSelector;
import android.support.v17.leanback.widget.HeaderItem;
import android.support.v17.leanback.widget.ListRow;
import android.support.v17.leanback.widget.ListRowPresenter;
import android.support.v17.leanback.widget.ObjectAdapter;
import android.support.v17.leanback.widget.OnActionClickedListener;
import android.support.v17.leanback.widget.PlaybackControlsRow;
import android.support.v17.leanback.widget.PlaybackControlsRowPresenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.SparseArrayObjectAdapter;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;
import de.nicidienase.chaosflix.CardPresenter;
@ -32,11 +28,6 @@ import de.nicidienase.chaosflix.entities.recording.Recording;
import de.nicidienase.chaosflix.entities.streaming.Room;
import de.nicidienase.chaosflix.entities.streaming.StreamUrl;
import static android.support.v17.leanback.app.PlaybackControlSupportGlue.ACTION_FAST_FORWARD;
import static android.support.v17.leanback.app.PlaybackControlSupportGlue.ACTION_PLAY_PAUSE;
import static android.support.v17.leanback.app.PlaybackControlSupportGlue.ACTION_REWIND;
import static android.support.v17.leanback.app.PlaybackControlSupportGlue.ACTION_SKIP_TO_NEXT;
import static android.support.v17.leanback.app.PlaybackControlSupportGlue.ACTION_SKIP_TO_PREVIOUS;
import static android.support.v4.media.session.MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS;
import static android.support.v4.media.session.MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS;
@ -62,11 +53,6 @@ public class ExoOverlayFragment extends PlaybackFragment{
private int eventType;
private StreamUrl mSelectedStream;
private PlaybackControlsRow.PlayPauseAction mPlayPauseAction;
private PlaybackControlsRow.FastForwardAction mFastForwardAction;
private PlaybackControlsRow.RewindAction mRewindAction;
private PlaybackControlsRow.SkipNextAction mSkipNextAction;
private PlaybackControlsRow.SkipPreviousAction mSkipPreviousAction;
private MediaController.Callback mMediaControllerCallback;
public interface PlaybackControlListener {
@ -108,6 +94,7 @@ public class ExoOverlayFragment extends PlaybackFragment{
setBackgroundType(PlaybackFragment.BG_LIGHT);
setFadingEnabled(false);
mHelper.setFadingEnabled(true);
}
@Override
@ -158,6 +145,13 @@ public class ExoOverlayFragment extends PlaybackFragment{
return 0;
}
private long getCurrentPositionLong(){
if(mCallback != null){
return mCallback.getCurrentPosition();
}
return 0;
}
public long getCurrentBufferedPosition(){
if(mCallback != null){
return mCallback.getBufferedPosition();
@ -176,6 +170,7 @@ public class ExoOverlayFragment extends PlaybackFragment{
super.onDestroy();
mSession.release();
mCallback.releasePlayer();
mHelper.onStop();
}
@SuppressWarnings("WrongConstant")
@ -195,11 +190,8 @@ public class ExoOverlayFragment extends PlaybackFragment{
mSession.setFlags(FLAG_HANDLES_MEDIA_BUTTONS| FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setActive(true);
PlaybackState state = new PlaybackState.Builder()
.setActions(getAvailableActions())
.setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 0)
.build();
mSession.setPlaybackState(state);
setPlaybackState(PlaybackState.STATE_NONE);
// mSession.setPlaybackState(state);
getActivity().setMediaController(
new MediaController(getActivity(),mSession.getSessionToken()));
@ -207,43 +199,70 @@ public class ExoOverlayFragment extends PlaybackFragment{
}
private void setPlaybackState(int state){
int currentPosition = getCurrentPosition();
long currentPosition = getCurrentPositionLong();
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(state,currentPosition,1.0f);
.setActions(getAvailableActions(state))
.setState(PlaybackState.STATE_PLAYING, currentPosition, 0);
mSession.setPlaybackState(stateBuilder.build());
}
private long getAvailableActions() {
private int getPlaybackState() {
Activity activity = getActivity();
if (activity != null) {
PlaybackState state = activity.getMediaController().getPlaybackState();
if (state != null) {
return state.getState();
} else {
return PlaybackState.STATE_NONE;
}
}
return PlaybackState.STATE_NONE;
}
private long getAvailableActions(int nextState) {
long actions = PlaybackState.ACTION_PLAY |
PlaybackState.ACTION_SKIP_TO_NEXT |
PlaybackState.ACTION_SKIP_TO_PREVIOUS |
PlaybackState.ACTION_FAST_FORWARD |
PlaybackState.ACTION_REWIND |
PlaybackState.ACTION_PAUSE;
if (nextState == PlaybackState.STATE_PLAYING) {
actions |= PlaybackState.ACTION_PAUSE;
}
return actions;
}
private class ChaosflixSessionCallback extends MediaSession.Callback {
@Override
public void onPlay() {
setPlaybackState(PlaybackState.STATE_PLAYING);
mCallback.play();
}
@Override
public void onPause() {
setPlaybackState(PlaybackState.STATE_PAUSED);
mCallback.pause();
}
@Override
public void onFastForward() {
int prevState = getPlaybackState();
setPlaybackState(PlaybackState.STATE_FAST_FORWARDING);
mCallback.skipForward(30);
setPlaybackState(prevState);
}
@Override
public void onRewind() {
int prevState = getPlaybackState();
setPlaybackState(PlaybackState.STATE_FAST_FORWARDING);
mCallback.skipBackward(30);
setPlaybackState(prevState);
}
@Override
@ -261,18 +280,4 @@ public class ExoOverlayFragment extends PlaybackFragment{
mCallback.seekTo(pos);
}
}
private class ChaosflixActionClickListener implements OnActionClickedListener {
@Override
public void onActionClicked(Action action) {
if(action.getId() == mPlayPauseAction.getId()){
if(mPlayPauseAction.getIndex() == PlaybackControlsRow.PlayPauseAction.PLAY){
mCallback.play();
} else if(mPlayPauseAction.getIndex() == PlaybackControlsRow.PlayPauseAction.PAUSE){
mCallback.pause();
}
}
}
}
}

View file

@ -14,11 +14,14 @@
package de.nicidienase.chaosflix.fragments;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.app.PlaybackFragment;
import android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter;
import android.support.v17.leanback.widget.Action;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
@ -30,6 +33,7 @@ import android.support.v17.leanback.widget.OnActionClickedListener;
import android.support.v17.leanback.widget.PlaybackControlsRow;
import android.support.v17.leanback.widget.PlaybackControlsRow.FastForwardAction;
import android.support.v17.leanback.widget.PlaybackControlsRow.PlayPauseAction;
import android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction;
import android.support.v17.leanback.widget.PlaybackControlsRow.RewindAction;
import android.support.v17.leanback.widget.PlaybackControlsRowPresenter;
import android.widget.Toast;