diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2020-10-25 19:00:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-25 19:00:55 +0100 |
commit | a0c604dc869f5c918d96c67a6b8a30bf1c71dce4 (patch) | |
tree | 50dd15375d7d0b9baa3fc581f0b604983fa3f450 | |
parent | aa3a350e38df8b98249543b2e63f95af9f6e6da5 (diff) | |
parent | 3f80a5fdb8c46e21184917bc947e022f6dee73b4 (diff) | |
download | AntennaPod-a0c604dc869f5c918d96c67a6b8a30bf1c71dce4.zip |
Merge pull request #4591 from ByteHamster/ensure-background-thread
Ensure that first DB query is executed in background thread
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/service/playback/PlaybackService.java | 76 |
1 files changed, 46 insertions, 30 deletions
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 92f395d0d..b85998538 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 @@ -502,16 +502,27 @@ public class PlaybackService extends MediaBrowserServiceCompat { stateManager.stopService(); return Service.START_NOT_STICKY; } - if (playable instanceof FeedMedia) { - playable = DBReader.getFeedMedia(((FeedMedia) playable).getId()); - } - if (playable == null) { - Log.d(TAG, "Playable was not found. Stopping service."); - stateManager.stopService(); - return Service.START_NOT_STICKY; - } - mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, prepareImmediately); - addPlayableToQueue(playable); + + Observable.fromCallable( + () -> { + if (playable instanceof FeedMedia) { + return DBReader.getFeedMedia(((FeedMedia) playable).getId()); + } else { + return playable; + } + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + playableLoaded -> { + mediaPlayer.playMediaObject(playable, stream, startWhenPrepared, + prepareImmediately); + addPlayableToQueue(playable); + }, error -> { + Log.d(TAG, "Playable was not found. Stopping service."); + stateManager.stopService(); + }); + return Service.START_NOT_STICKY; } else { Log.d(TAG, "Did not handle intent to PlaybackService: " + intent); Log.d(TAG, "Extras: " + intent.getExtras()); @@ -680,26 +691,31 @@ public class PlaybackService extends MediaBrowserServiceCompat { } private void startPlayingFromPreferences() { - Playable playable = Playable.PlayableUtils.createInstanceFromPreferences(getApplicationContext()); - if (playable != null) { - if (PlaybackPreferences.getCurrentEpisodeIsStream() && !NetworkUtils.isStreamingAllowed()) { - displayStreamingNotAllowedNotification( - new PlaybackServiceStarter(this, playable) - .prepareImmediately(true) - .startWhenPrepared(true) - .shouldStream(true) - .getIntent()); - PlaybackPreferences.writeNoMediaPlaying(); - stateManager.stopService(); - return; - } - mediaPlayer.playMediaObject(playable, PlaybackPreferences.getCurrentEpisodeIsStream(), true, true); - stateManager.validStartCommandWasReceived(); - PlaybackService.this.updateMediaSessionMetadata(playable); - addPlayableToQueue(playable); - } else { - stateManager.stopService(); - } + Observable.fromCallable(() -> Playable.PlayableUtils.createInstanceFromPreferences(getApplicationContext())) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + playable -> { + if (PlaybackPreferences.getCurrentEpisodeIsStream() && !NetworkUtils.isStreamingAllowed()) { + displayStreamingNotAllowedNotification( + new PlaybackServiceStarter(this, playable) + .prepareImmediately(true) + .startWhenPrepared(true) + .shouldStream(true) + .getIntent()); + PlaybackPreferences.writeNoMediaPlaying(); + stateManager.stopService(); + return; + } + mediaPlayer.playMediaObject(playable, PlaybackPreferences.getCurrentEpisodeIsStream(), + true, true); + stateManager.validStartCommandWasReceived(); + PlaybackService.this.updateMediaSessionMetadata(playable); + addPlayableToQueue(playable); + }, error -> { + Log.d(TAG, "Playable was not loaded from preferences. Stopping service."); + stateManager.stopService(); + }); } /** |