From 0f32e2953ce2aed89e676d815fa9cb2494a31e1a Mon Sep 17 00:00:00 2001 From: Domingos Lopes Date: Sat, 30 Apr 2016 21:01:14 -0400 Subject: [PATCH] avoid stopping playback after skipping (and changing media type) --- .../PlaybackServiceMediaPlayerTest.java | 26 +++++++++---------- .../activity/MediaplayerActivity.java | 4 +-- .../activity/VideoplayerActivity.java | 14 +++++++--- .../core/service/playback/LocalPSMP.java | 13 +++++++--- .../service/playback/PlaybackService.java | 8 +++--- .../playback/PlaybackServiceMediaPlayer.java | 2 +- .../core/service/playback/RemotePSMP.java | 4 +-- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java index 195dccdda..04be78f46 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java @@ -176,7 +176,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -255,7 +255,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -337,7 +337,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -420,7 +420,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -497,7 +497,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -575,7 +575,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -655,7 +655,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -738,7 +738,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -796,7 +796,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -874,7 +874,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -989,7 +989,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -1081,7 +1081,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } @@ -1185,7 +1185,7 @@ public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { } diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java index 8ee91c7a3..71d288725 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java @@ -211,11 +211,11 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements @Override protected void onPause() { - super.onPause(); if(controller != null) { controller.reinitServiceIfPaused(); controller.pause(); } + super.onPause(); } /** @@ -257,12 +257,12 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements @Override protected void onStop() { - super.onStop(); Log.d(TAG, "onStop()"); if (controller != null) { controller.release(); controller = null; // prevent leak } + super.onStop(); } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) diff --git a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java index a52382dea..a4ffebae2 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/VideoplayerActivity.java @@ -41,6 +41,7 @@ public class VideoplayerActivity extends MediaplayerActivity { */ private boolean videoControlsShowing = true; private boolean videoSurfaceCreated = false; + private boolean destroyingDueToReload = false; private VideoControlsHider videoControlsHider = new VideoControlsHider(this); @@ -86,6 +87,7 @@ public class VideoplayerActivity extends MediaplayerActivity { } else if (PlaybackService.isCasting()) { Intent intent = PlaybackService.getPlayerActivityIntent(this); if (!intent.getComponent().getClassName().equals(VideoplayerActivity.class.getName())) { + destroyingDueToReload = true; finish(); startActivity(intent); } @@ -94,18 +96,18 @@ public class VideoplayerActivity extends MediaplayerActivity { @Override protected void onPause() { - super.onPause(); videoControlsHider.stop(); if (controller != null && controller.getStatus() == PlayerStatus.PLAYING) { controller.pause(); } + super.onPause(); } @Override protected void onDestroy() { - super.onDestroy(); videoControlsHider.stop(); videoControlsHider = null; + super.onDestroy(); } @Override @@ -242,7 +244,7 @@ public class VideoplayerActivity extends MediaplayerActivity { if (controller.serviceAvailable()) { controller.setVideoSurface(holder); } else { - Log.e(TAG, "Could'nt attach surface to mediaplayer - reference to service was null"); + Log.e(TAG, "Couldn't attach surface to mediaplayer - reference to service was null"); } } @@ -252,7 +254,9 @@ public class VideoplayerActivity extends MediaplayerActivity { public void surfaceDestroyed(SurfaceHolder holder) { Log.d(TAG, "Videosurface was destroyed"); videoSurfaceCreated = false; - controller.notifyVideoSurfaceAbandoned(); + if (!destroyingDueToReload) { + controller.notifyVideoSurfaceAbandoned(); + } } }; @@ -261,10 +265,12 @@ public class VideoplayerActivity extends MediaplayerActivity { protected void onReloadNotification(int notificationCode) { if (notificationCode == PlaybackService.EXTRA_CODE_AUDIO) { Log.d(TAG, "ReloadNotification received, switching to Audioplayer now"); + destroyingDueToReload = true; finish(); startActivity(new Intent(this, AudioplayerActivity.class)); } else if (notificationCode == PlaybackService.EXTRA_CODE_CAST) { Log.d(TAG, "ReloadNotification received, switching to Castplayer now"); + destroyingDueToReload = true; finish(); startActivity(new Intent(this, CastplayerActivity.class)); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java index be80ea112..acd0ccfdb 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/LocalPSMP.java @@ -158,7 +158,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { setPlayerStatus(PlayerStatus.INITIALIZING, media); try { media.loadMetadata(); - callback.reloadUI(); + callback.reloadUI(true); if (stream) { mediaPlayer.setDataSource(media.getStreamUrl()); } else { @@ -337,6 +337,7 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { public void reinit() { executor.submit(() -> { playerLock.lock(); + Log.d(TAG, "reinit()"); releaseWifiLockIfNecessary(); if (media != null) { playMediaObject(media, true, stream, startWhenPrepared.get(), false); @@ -629,9 +630,13 @@ public class LocalPSMP extends PlaybackServiceMediaPlayer { public void resetVideoSurface() { executor.submit(() -> { playerLock.lock(); - Log.d(TAG, "Resetting video surface"); - mediaPlayer.setDisplay(null); - reinit(); + if (mediaType == MediaType.VIDEO) { + Log.d(TAG, "Resetting video surface"); + mediaPlayer.setDisplay(null); + reinit(); + } else { + Log.e(TAG, "Resetting video surface for media of Audio type"); + } playerLock.unlock(); }); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java index 2e4a486e6..17473feb3 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java @@ -499,7 +499,7 @@ public class PlaybackService extends Service { } public void notifyVideoSurfaceAbandoned() { - stopForeground(true); + stopForeground(!UserPreferences.isPersistNotify()); mediaPlayer.resetVideoSurface(); } @@ -640,9 +640,11 @@ public class PlaybackService extends Service { } @Override - public void reloadUI() { + public void reloadUI(boolean onlyMediaSession) { Log.d(TAG, "reloadUI callback reached"); - sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); + if (!onlyMediaSession) { + sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, 0); + } PlaybackService.this.updateMediaSessionMetadata(getPlayable()); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java index f3a9c1d03..132dd095b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackServiceMediaPlayer.java @@ -322,7 +322,7 @@ public abstract class PlaybackServiceMediaPlayer { void onBufferingUpdate(int percent); - void reloadUI(); + void reloadUI(boolean onlyMediaSession); boolean onMediaPlayerInfo(int code, @StringRes int resourceId); diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/playback/RemotePSMP.java b/core/src/main/java/de/danoeh/antennapod/core/service/playback/RemotePSMP.java index b9068447e..5d7c53b23 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/playback/RemotePSMP.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/playback/RemotePSMP.java @@ -244,7 +244,7 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); } if (updateUI) { - callback.reloadUI(); + callback.reloadUI(false); } } @@ -302,7 +302,7 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { setPlayerStatus(PlayerStatus.INITIALIZING, media); try { media.loadMetadata(); - callback.reloadUI(); + callback.reloadUI(false); setPlayerStatus(PlayerStatus.INITIALIZED, media); if (prepareImmediately) { prepare();