From 04b86c872e48821cc5e3a92ece407eedb03c4ab3 Mon Sep 17 00:00:00 2001 From: Felix Date: Mon, 27 Mar 2017 00:36:38 +0200 Subject: [PATCH] add controls --- .../activities/ExoPlayerActivity.java | 49 +++++++++++++------ .../fragments/ExoOverlayFragment.java | 11 ++++- .../main/res/layout/exoplayback_activity.xml | 2 +- 3 files changed, 45 insertions(+), 17 deletions(-) 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 ca718d33..4785ba49 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java +++ b/app/src/main/java/de/nicidienase/chaosflix/activities/ExoPlayerActivity.java @@ -4,9 +4,9 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.support.annotation.Nullable; -import android.support.v17.leanback.app.PlaybackFragment; import android.text.TextUtils; -import android.view.TextureView; +import android.util.Log; +import android.view.SurfaceView; import android.view.View; import com.google.android.exoplayer2.C; @@ -15,7 +15,6 @@ import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; -import com.google.android.exoplayer2.extractor.ExtractorsFactory; import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.dash.DashMediaSource; @@ -32,16 +31,12 @@ import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.HttpDataSource; -import com.google.android.exoplayer2.util.UriUtil; import com.google.android.exoplayer2.util.Util; import butterknife.BindView; import butterknife.ButterKnife; import de.nicidienase.chaosflix.R; -import de.nicidienase.chaosflix.entities.recording.Event; -import de.nicidienase.chaosflix.entities.recording.Recording; import de.nicidienase.chaosflix.fragments.ExoOverlayFragment; -import de.nicidienase.chaosflix.fragments.PlaybackOverlayFragment; /** * Created by felix on 26.03.17. @@ -50,8 +45,9 @@ import de.nicidienase.chaosflix.fragments.PlaybackOverlayFragment; public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty implements ExoOverlayFragment.PlaybackControlListener{ + private static final String TAG = ExoPlayerActivity.class.getSimpleName(); @BindView(R.id.videoView) - TextureView mTextureView; + SurfaceView mSurfaceView; @BindView(R.id.playback_controls_fragment) View mPlaybackControllFragment; private DefaultBandwidthMeter bandwidthMeter; @@ -59,6 +55,7 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty private String mUserAgent; private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter(); + private Handler mainHandler; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -74,10 +71,16 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty } } + @Override + protected void onPause() { + super.onPause(); + pause(); + } + private void setupPlayer(){ mUserAgent = Util.getUserAgent(this, getResources().getString(R.string.app_name)); - Handler mainHander = new Handler(); + mainHandler = new Handler(); bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); @@ -88,11 +91,12 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); - player.setVideoTextureView(mTextureView); + player.setVideoSurfaceView(mSurfaceView); } @Override public void setVideoSource(String source) { + Log.d(TAG,"Source: " + source); synchronized (this){ if(player == null){ setupPlayer(); @@ -106,16 +110,33 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty @Override public void play() { + player.setPlayWhenReady(true); } @Override public void pause() { + player.setPlayWhenReady(false); } @Override public void playPause() { + player.setPlayWhenReady(!player.getPlayWhenReady()); + } + @Override + public void seekTo(int sec) { + player.seekTo(sec * 1000); + } + + @Override + public void skipForward(int sec){ + player.seekTo(player.getCurrentPosition()+(sec*1000)); + } + + @Override + public void skipBackward(int sec){ + player.seekTo(player.getCurrentPosition()-(sec*1000)); } private MediaSource buildMediaSource(Uri uri, String overrideExtension) { @@ -125,15 +146,15 @@ public class ExoPlayerActivity extends AbstractServiceConnectedAcitivty switch (type) { case C.TYPE_SS: return new SsMediaSource(uri, buildDataSourceFactory(false), - new DefaultSsChunkSource.Factory(mediaDataSourceFactory), null, null); + new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mainHandler, null); case C.TYPE_DASH: return new DashMediaSource(uri, buildDataSourceFactory(false), - new DefaultDashChunkSource.Factory(mediaDataSourceFactory), null, null); + new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mainHandler, null); case C.TYPE_HLS: - return new HlsMediaSource(uri, mediaDataSourceFactory, null, null); + return new HlsMediaSource(uri, mediaDataSourceFactory, mainHandler, null); case C.TYPE_OTHER: return new ExtractorMediaSource(uri, mediaDataSourceFactory, new DefaultExtractorsFactory(), - null, null); + mainHandler, null); default: { throw new IllegalStateException("Unsupported type: " + type); } 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 931ca6fe..d8d6302c 100644 --- a/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java +++ b/app/src/main/java/de/nicidienase/chaosflix/fragments/ExoOverlayFragment.java @@ -34,6 +34,9 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac void pause(); void playPause(); void setVideoSource(String source); + void skipForward(int sec); + void skipBackward(int sec); + void seekTo(int sec); } @Override @@ -53,8 +56,6 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac ArrayObjectAdapter rowsAdapter = setupRows(); // rowsAdapter.add(getRelatedItems()); setAdapter(rowsAdapter); - - mCallback.setVideoSource(mSelectedRecording.getUrl()); } private ArrayObjectAdapter setupRows() { @@ -82,6 +83,12 @@ public class ExoOverlayFragment extends android.support.v17.leanback.app.Playbac return 0; } + @Override + public void onStart() { + super.onStart(); + mCallback.setVideoSource(mSelectedRecording.getRecordingUrl()); + } + @Override public void onAttach(Activity context) { super.onAttach(context); diff --git a/app/src/main/res/layout/exoplayback_activity.xml b/app/src/main/res/layout/exoplayback_activity.xml index 774fbdb1..cb42fda0 100644 --- a/app/src/main/res/layout/exoplayback_activity.xml +++ b/app/src/main/res/layout/exoplayback_activity.xml @@ -3,7 +3,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> -