From a6ce0a570dee3a6880e44224d6ff0b4b8593b84a Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Sat, 15 Dec 2018 21:59:59 +0100 Subject: [PATCH] - Video player moved into service - Video notification and background playback --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 1 + .../peertube/activity/VideoPlayActivity.java | 272 ++++++++++-------- .../peertube/adapter/VideoAdapter.java | 8 +- .../peertube/service/VideoPlayerService.java | 187 ++++++++++++ app/src/main/res/drawable/ic_peertube_bw.xml | 16 ++ .../main/res/layout/activity_video_play.xml | 57 +++- .../res/layout/exo_playback_control_view.xml | 74 +++++ app/src/main/res/layout/row_video.xml | 16 +- app/src/main/res/values/strings.xml | 1 + 10 files changed, 508 insertions(+), 130 deletions(-) create mode 100644 app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java create mode 100644 app/src/main/res/drawable/ic_peertube_bw.xml create mode 100644 app/src/main/res/layout/exo_playback_control_view.xml diff --git a/app/build.gradle b/app/build.gradle index 7fa855a..fd7ddba 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,7 +44,11 @@ android { // implementation 'org.webrtc:google-webrtc:1.0.+' // video player - implementation 'com.google.android.exoplayer:exoplayer:2.8.1' + implementation 'com.google.android.exoplayer:exoplayer-core:2.8.1' + implementation 'com.google.android.exoplayer:exoplayer-dash:2.8.1' + implementation 'com.google.android.exoplayer:exoplayer-ui:2.8.1' + implementation 'com.google.android.exoplayer:exoplayer-hls:2.8.1' + implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.8.1' // implementation 'com.devbrackets.android:exomedia:4.1.0' // testing diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 44be243..178aa69 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -50,5 +50,6 @@ android:exported="false" /> + \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java index 5d8f94e..d886539 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java @@ -1,20 +1,27 @@ package net.schueller.peertube.activity; +import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.os.Environment; +import android.os.IBinder; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.PopupMenu; import android.util.Log; import android.view.Surface; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; +import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; @@ -25,32 +32,21 @@ import com.github.se_bastiaan.torrentstream.Torrent; import com.github.se_bastiaan.torrentstream.TorrentOptions; import com.github.se_bastiaan.torrentstream.TorrentStream; import com.github.se_bastiaan.torrentstream.listeners.TorrentListener; -import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.Format; -import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.decoder.DecoderCounters; -import com.google.android.exoplayer2.source.ExtractorMediaSource; -import com.google.android.exoplayer2.source.MediaSource; -import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; -import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; -import com.google.android.exoplayer2.trackselection.TrackSelection; -import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.PlayerView; -import com.google.android.exoplayer2.upstream.BandwidthMeter; -import com.google.android.exoplayer2.upstream.DataSource; -import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter; -import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.video.VideoRendererEventListener; - +import com.squareup.picasso.Picasso; import net.schueller.peertube.R; import net.schueller.peertube.helper.APIUrlHelper; import net.schueller.peertube.helper.MetaDataHelper; +import net.schueller.peertube.intents.Intents; +import net.schueller.peertube.model.Avatar; import net.schueller.peertube.model.Video; - import net.schueller.peertube.network.GetVideoDataService; import net.schueller.peertube.network.RetrofitInstance; - +import net.schueller.peertube.service.VideoPlayerService; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -61,95 +57,61 @@ public class VideoPlayActivity extends AppCompatActivity implements VideoRendere private ProgressBar progressBar; private PlayerView simpleExoPlayerView; - private SimpleExoPlayer player; + private Intent videoPlayerIntent; + private Context context = this; + + boolean mBound = false; + VideoPlayerService mService; + + private ServiceConnection mConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + Log.d(TAG, "onServiceConnected"); + VideoPlayerService.LocalBinder binder = (VideoPlayerService.LocalBinder) service; + mService = binder.getService(); + + // 2. Create the player + simpleExoPlayerView.setPlayer(mService.player); + mBound = true; + + loadVideo(); + } + + @Override + public void onServiceDisconnected(ComponentName componentName) { + Log.d(TAG, "onServiceDisconnected"); + simpleExoPlayerView.setPlayer(null); + mBound = false; + } + }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_play); - // get video ID - Intent intent = getIntent(); - String videoID = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID); - Log.v(TAG, "click: " + videoID); - progressBar = findViewById(R.id.progress); progressBar.setMax(100); -// PlayerView videoView = findViewById(R.id.video_view); simpleExoPlayerView = new PlayerView(this); simpleExoPlayerView = findViewById(R.id.video_view); - // 1. Create a default TrackSelector - BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); - TrackSelection.Factory videoTrackSelectionFactory = - new AdaptiveTrackSelection.Factory(bandwidthMeter); - TrackSelector trackSelector = - new DefaultTrackSelector(videoTrackSelectionFactory); - - // 2. Create the player - player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), trackSelector); - simpleExoPlayerView.setPlayer(player); - - // get video details from api - String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); - GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); - - Call