diff --git a/app/src/main/java/de/nicidienase/chaosflix/PlaybackHelper.java b/app/src/main/java/de/nicidienase/chaosflix/PlaybackHelper.java index aae1dd66..57ddae24 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/PlaybackHelper.java +++ b/app/src/main/java/de/nicidienase/chaosflix/PlaybackHelper.java @@ -5,17 +5,20 @@ import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.session.MediaController; +import android.os.Handler; import android.support.v17.leanback.app.PlaybackControlGlue; +import android.view.View; import com.bumptech.glide.Glide; -import java.util.concurrent.ExecutionException; - 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.Stream; import de.nicidienase.chaosflix.fragments.ExoOverlayFragment; +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; /** * Created by felix on 26.03.17. @@ -24,14 +27,18 @@ import de.nicidienase.chaosflix.fragments.ExoOverlayFragment; public class PlaybackHelper extends PlaybackControlGlue { private static final int[] SEEK_SPEEDS = {2, 4, 8, 16}; + private static final int DEFAULT_UPDATE_PERIOD = 500; + private static final int UPDATE_PERIOD = 16; private static final String TAG = PlaybackHelper.class.getSimpleName(); private final Context mContext; private final ExoOverlayFragment mFragment; + private BitmapDrawable mDrawable = null; private Room room; private Stream stream; private Event event; private Recording recording; - private MediaController mMediaController; + private Runnable mUpdateProgressRunnable; + private Handler mHandler = new Handler(); public PlaybackHelper(Context context, ExoOverlayFragment fragment, Event event, Recording recording){ super(context, fragment,SEEK_SPEEDS); @@ -39,6 +46,19 @@ public class PlaybackHelper extends PlaybackControlGlue { this.mFragment = fragment; this.event = event; this.recording = recording; + + 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, Stream stream ){ @@ -91,22 +111,49 @@ public class PlaybackHelper extends PlaybackControlGlue { @Override public Drawable getMediaArt() { - if(mediaIsRecording()){ - try { - return new BitmapDrawable( - mContext.getResources(), - Glide.with(getContext()) - .load(event.getThumbUrl()) - .asBitmap() - .into(-1, -1) - .get()); - } catch (InterruptedException e) { - e.printStackTrace(); - } catch (ExecutionException e) { - e.printStackTrace(); - } + return mDrawable; + } + + @Override + public int getUpdatePeriod() { + View view = mFragment.getView(); + int totalTime = getControlsRow().getTotalTime(); + if (view == null || totalTime <= 0 || view.getWidth() == 0) { + return DEFAULT_UPDATE_PERIOD; + } + return Math.max(UPDATE_PERIOD, totalTime / view.getWidth()); + } + + @Override + public void updateProgress() { + if (mUpdateProgressRunnable == null) { + mUpdateProgressRunnable = new Runnable() { + @Override + public void run() { + int totalTime = getControlsRow().getTotalTime(); + int currentTime = getCurrentPosition(); + getControlsRow().setCurrentTime(currentTime); + + int progress = mFragment.getCurrentPosition(); + getControlsRow().setBufferedProgress(progress); + + if (totalTime > 0 && totalTime <= currentTime) { + stopProgressAnimation(); + } else { + updateProgress(); + } + } + }; + } + + mHandler.postDelayed(mUpdateProgressRunnable, getUpdatePeriod()); + } + + private void stopProgressAnimation() { + if (mHandler != null && mUpdateProgressRunnable != null) { + mHandler.removeCallbacks(mUpdateProgressRunnable); + mUpdateProgressRunnable = null; } - return null; } @Override diff --git a/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java b/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java index 4785ba49..5d726d73 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java +++ b/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java @@ -6,6 +6,7 @@ import android.os.Handler; import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; +import android.view.KeyEvent; import android.view.SurfaceView; import android.view.View; @@ -48,8 +49,7 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty private static final String TAG = ExoPlayerActivity.class.getSimpleName(); @BindView(R.id.videoView) SurfaceView mSurfaceView; - @BindView(R.id.playback_controls_fragment) - View mPlaybackControllFragment; + ExoOverlayFragment mPlaybackControllFragment; private DefaultBandwidthMeter bandwidthMeter; private SimpleExoPlayer player; private String mUserAgent; @@ -63,6 +63,8 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty setContentView(R.layout.exoplayback_activity); ButterKnife.bind(this); + mPlaybackControllFragment = (ExoOverlayFragment) getFragmentManager().findFragmentById(R.id.playback_controls_fragment); + mUserAgent = Util.getUserAgent(this, getResources().getString(R.string.app_name)); synchronized (this){ if(player == null){ @@ -71,10 +73,17 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty } } + @Override + protected void onStart() { + super.onStart(); + mPlaybackControllFragment.startEntranceTransition(); + } + @Override protected void onPause() { super.onPause(); pause(); + // TODO persist playback progress } private void setupPlayer(){ @@ -129,6 +138,24 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty player.seekTo(sec * 1000); } + @Override + public boolean isMediaPlaying() { + if(player != null){ + return player.getPlayWhenReady(); + } else { + return false; + } + } + + @Override + public long getCurrentPosition() { + if(player != null){ + return player.getCurrentPosition(); + } else { + return 0; + } + } + @Override public void skipForward(int sec){ player.seekTo(player.getCurrentPosition()+(sec*1000)); diff --git a/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java b/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java index d8d6302c..b97651b0 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java +++ b/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java @@ -1,7 +1,6 @@ package de.nicidienase.chaosflix.fragments; import android.app.Activity; -import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v17.leanback.app.PlaybackFragment; @@ -28,6 +27,7 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac private Event mSelectedEvent; private PlaybackHelper mHelper; private PlaybackControlListener mCallback; + private ArrayObjectAdapter mRowsAdapter; public interface PlaybackControlListener { void play(); @@ -37,6 +37,8 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac void skipForward(int sec); void skipBackward(int sec); void seekTo(int sec); + boolean isMediaPlaying(); + long getCurrentPosition(); } @Override @@ -53,18 +55,17 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac mHelper = new PlaybackHelper(getActivity(),this,mSelectedEvent,mSelectedRecording); - ArrayObjectAdapter rowsAdapter = setupRows(); -// rowsAdapter.add(getRelatedItems()); - setAdapter(rowsAdapter); - } + PlaybackControlsRowPresenter playbackControlsRowPresenter + = mHelper.createControlsRowAndPresenter(); + PlaybackControlsRow controlsRow = mHelper.getControlsRow(); - private ArrayObjectAdapter setupRows() { ClassPresenterSelector ps = new ClassPresenterSelector(); - PlaybackControlsRowPresenter playbackControlsRowPresenter; - playbackControlsRowPresenter = mHelper.getControlsRowPresenter(); ps.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter); ps.addClassPresenter(ListRow.class, new ListRowPresenter()); - return new ArrayObjectAdapter(ps); + mRowsAdapter = new ArrayObjectAdapter(ps); + mRowsAdapter.add(controlsRow); +// mRowsAdapter.add(getRelatedItems()); + setAdapter(mRowsAdapter); } private Row getRelatedItems() { @@ -76,10 +77,16 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac } public boolean isMediaPlaying() { + if(mCallback != null){ + return mCallback.isMediaPlaying(); + } return false; } public int getCurrentPosition() { + if(mCallback != null){ + return (int) mCallback.getCurrentPosition(); + } return 0; }