From de7ca4f451e530963043e22e8492e461d6d2d63f Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Mon, 17 Dec 2018 18:54:09 +0100 Subject: [PATCH 1/7] - Fixed crash when stopping playback in notification - Audio output change detection (headphones unplugged) - Android 5.0 Support (untested) --- app/build.gradle | 16 +++---- .../peertube/activity/VideoPlayActivity.java | 6 ++- .../peertube/service/VideoPlayerService.java | 44 +++++++++++++++---- 3 files changed, 47 insertions(+), 19 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index fd7ddba..2b761af 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,10 +4,10 @@ android { compileSdkVersion 28 defaultConfig { applicationId "net.schueller.peertube" - minSdkVersion 23 + minSdkVersion 21 targetSdkVersion 28 - versionCode 106 - versionName "1.0.6" + versionCode 107 + versionName "1.0.7" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) @@ -44,11 +44,11 @@ android { // implementation 'org.webrtc:google-webrtc:1.0.+' // video player - 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.google.android.exoplayer:exoplayer-core:2.9.2' + implementation 'com.google.android.exoplayer:exoplayer-dash:2.9.2' + implementation 'com.google.android.exoplayer:exoplayer-ui:2.9.2' + implementation 'com.google.android.exoplayer:exoplayer-hls:2.9.2' + implementation 'com.google.android.exoplayer:exoplayer-smoothstreaming:2.9.2' // implementation 'com.devbrackets.android:exomedia:4.1.0' // testing 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 d886539..e4ee25d 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java @@ -355,8 +355,10 @@ public class VideoPlayActivity extends AppCompatActivity implements VideoRendere @Override protected void onStop() { super.onStop(); - unbindService(mConnection); - mBound = false; + if (mBound) { + unbindService(mConnection); + mBound = false; + } Log.v(TAG, "onStop()..."); } diff --git a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java index 85f3286..5f6fcd1 100644 --- a/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java +++ b/app/src/main/java/net/schueller/peertube/service/VideoPlayerService.java @@ -1,19 +1,20 @@ package net.schueller.peertube.service; - import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.Bitmap; +import android.media.AudioManager; import android.net.Uri; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; - import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; @@ -23,16 +24,14 @@ import com.google.android.exoplayer2.ui.PlayerNotificationManager; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.util.Util; -import com.squareup.picasso.Picasso; import net.schueller.peertube.R; import net.schueller.peertube.activity.VideoPlayActivity; import net.schueller.peertube.helper.MetaDataHelper; import net.schueller.peertube.model.Video; -import java.io.IOException; - -import retrofit2.http.FormUrlEncoded; +import static android.media.session.PlaybackState.ACTION_PAUSE; +import static android.media.session.PlaybackState.ACTION_PLAY; public class VideoPlayerService extends Service { @@ -48,14 +47,32 @@ public class VideoPlayerService extends Service { private PlayerNotificationManager playerNotificationManager; + private IntentFilter becomeNoisyIntentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); + private BecomingNoisyReceiver myNoisyAudioStreamReceiver = new BecomingNoisyReceiver(); + @Override public void onCreate() { super.onCreate(); - Log.v("VideoPlayerService", "onCreate..."); - player = ExoPlayerFactory.newSimpleInstance(getApplicationContext(), new DefaultTrackSelector()); + // Stop player if audio device changes, e.g. headphones unplugged + player.addListener(new Player.EventListener() { + @Override + public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { + + if (playbackState == ACTION_PAUSE) { // this means that pause is available, hence the audio is playing + Log.v("VideoPlayerService", "ACTION_PLAY: " + playbackState); + registerReceiver(myNoisyAudioStreamReceiver, becomeNoisyIntentFilter); + } + + if (playbackState == ACTION_PLAY) { // this means that play is available, hence the audio is paused or stopped + Log.v("VideoPlayerService", "ACTION_PAUSE: " + playbackState); + unregisterReceiver(myNoisyAudioStreamReceiver); + } + } + } ); + } public class LocalBinder extends Binder { @@ -143,7 +160,6 @@ public class VideoPlayerService extends Service { PendingIntent.FLAG_UPDATE_CURRENT); } - @Nullable @Override public String getCurrentContentText(Player player) { return MetaDataHelper.getMetaString( @@ -184,4 +200,14 @@ public class VideoPlayerService extends Service { } + // pause playback on audio output change + private class BecomingNoisyReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { + player.setPlayWhenReady(false); + } + } + } + } From eeb2fcd5f0f25dccd5fd69e5cc3e10a3d4bed535 Mon Sep 17 00:00:00 2001 From: Stefan Schueller Date: Mon, 17 Dec 2018 21:07:49 +0100 Subject: [PATCH 2/7] - Fixed permission missing for Android Pie --- app/build.gradle | 4 ++-- app/src/main/AndroidManifest.xml | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2b761af..74ab3a8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "net.schueller.peertube" minSdkVersion 21 targetSdkVersion 28 - versionCode 107 - versionName "1.0.7" + versionCode 108 + versionName "1.0.8" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 178aa69..3343545 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,9 @@ + + + Date: Mon, 17 Dec 2018 21:32:01 +0100 Subject: [PATCH 3/7] - Restart video play activity with correct data on trigger from notification --- app/build.gradle | 4 ++-- .../net/schueller/peertube/activity/VideoPlayActivity.java | 6 +++--- .../net/schueller/peertube/service/VideoPlayerService.java | 5 +++++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 74ab3a8..d6c80a7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,8 +6,8 @@ android { applicationId "net.schueller.peertube" minSdkVersion 21 targetSdkVersion 28 - versionCode 108 - versionName "1.0.8" + versionCode 109 + versionName "1.0.9" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) 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 e4ee25d..b1cef54 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoPlayActivity.java @@ -206,14 +206,14 @@ public class VideoPlayActivity extends AppCompatActivity implements VideoRendere { // get video ID Intent intent = getIntent(); - String videoID = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID); - Log.v(TAG, "click: " + videoID); + String videoUuid = intent.getStringExtra(VideoListActivity.EXTRA_VIDEOID); + Log.v(TAG, "click: " + videoUuid); // get video details from api String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL).create(GetVideoDataService.class); - Call