From 40fc7b4816908e7bc85fd2e1be1602e3dafd76b2 Mon Sep 17 00:00:00 2001 From: Domingos Lopes Date: Sat, 26 Mar 2016 12:51:47 -0400 Subject: initialize mediaplayer according to local or remote device --- .../core/service/playback/PlaybackService.java | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'core/src/main/java/de') 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 d85bcb820..80007b862 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 @@ -260,10 +260,6 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar Log.d(TAG, "Service created."); isRunning = true; - CastManager castMgr = CastManager.getInstance(); - castMgr.addCastConsumer(castConsumer); - isCasting = castMgr.isConnected(); - registerReceiver(headsetDisconnected, new IntentFilter( Intent.ACTION_HEADSET_PLUG)); registerReceiver(shutdownReceiver, new IntentFilter( @@ -281,7 +277,14 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter( ACTION_RESUME_PLAY_CURRENT_EPISODE)); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); - mediaPlayer = new LocalPSMP(this, mediaPlayerCallback); + CastManager castMgr = CastManager.getInstance(); + castMgr.addCastConsumer(castConsumer); + isCasting = castMgr.isConnected(); + if (isCasting) { + onCastAppConnected(true); + } else { + mediaPlayer = new LocalPSMP(this, mediaPlayerCallback); + } ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); @@ -1561,20 +1564,7 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar private CastConsumer castConsumer = new CastConsumerImpl() { @Override public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) { - Log.d(TAG, "A cast device application was connected"); - isCasting = true; - if (mediaPlayer != null) { - PlaybackServiceMediaPlayer.PSMPInfo info = mediaPlayer.getPSMPInfo(); - if (info.playerStatus == PlayerStatus.PLAYING) { - // could be pause, but this way we make sure the new player will get the correct position, since pause runs asynchronously - saveCurrentPosition(false, 0); - } - } - switchMediaPlayer(new RemotePSMP(PlaybackService.this, mediaPlayerCallback), - (mediaPlayer != null) ? mediaPlayer.getPSMPInfo() : - new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null)); - sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, EXTRA_CODE_CAST); - registerWifiBroadcastReceiver(); + PlaybackService.this.onCastAppConnected(wasLaunched); } @Override @@ -1620,6 +1610,23 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar } }; + private void onCastAppConnected(boolean wasLaunched) { + Log.d(TAG, "A cast device application was connected"); + isCasting = true; + if (mediaPlayer != null) { + PlaybackServiceMediaPlayer.PSMPInfo info = mediaPlayer.getPSMPInfo(); + if (info.playerStatus == PlayerStatus.PLAYING) { + // could be pause, but this way we make sure the new player will get the correct position, since pause runs asynchronously + saveCurrentPosition(false, 0); + } + } + switchMediaPlayer(new RemotePSMP(PlaybackService.this, mediaPlayerCallback), + (mediaPlayer != null) ? mediaPlayer.getPSMPInfo() : + new PlaybackServiceMediaPlayer.PSMPInfo(PlayerStatus.STOPPED, null)); + sendNotificationBroadcast(NOTIFICATION_TYPE_RELOAD, EXTRA_CODE_CAST); + registerWifiBroadcastReceiver(); + } + private void switchMediaPlayer(@NonNull PlaybackServiceMediaPlayer newPlayer, @NonNull PlaybackServiceMediaPlayer.PSMPInfo info) { if (mediaPlayer != null) { -- cgit v1.2.3