initialize mediaplayer according to local or remote device

This commit is contained in:
Domingos Lopes 2016-03-26 12:51:47 -04:00
parent 1088fae745
commit 40fc7b4816
1 changed files with 26 additions and 19 deletions

View File

@ -260,10 +260,6 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar
Log.d(TAG, "Service created."); Log.d(TAG, "Service created.");
isRunning = true; isRunning = true;
CastManager castMgr = CastManager.getInstance();
castMgr.addCastConsumer(castConsumer);
isCasting = castMgr.isConnected();
registerReceiver(headsetDisconnected, new IntentFilter( registerReceiver(headsetDisconnected, new IntentFilter(
Intent.ACTION_HEADSET_PLUG)); Intent.ACTION_HEADSET_PLUG));
registerReceiver(shutdownReceiver, new IntentFilter( registerReceiver(shutdownReceiver, new IntentFilter(
@ -281,7 +277,14 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar
registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter( registerReceiver(pauseResumeCurrentEpisodeReceiver, new IntentFilter(
ACTION_RESUME_PLAY_CURRENT_EPISODE)); ACTION_RESUME_PLAY_CURRENT_EPISODE));
taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback); taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback);
CastManager castMgr = CastManager.getInstance();
castMgr.addCastConsumer(castConsumer);
isCasting = castMgr.isConnected();
if (isCasting) {
onCastAppConnected(true);
} else {
mediaPlayer = new LocalPSMP(this, mediaPlayerCallback); mediaPlayer = new LocalPSMP(this, mediaPlayerCallback);
}
ComponentName eventReceiver = new ComponentName(getApplicationContext(), ComponentName eventReceiver = new ComponentName(getApplicationContext(),
MediaButtonReceiver.class); MediaButtonReceiver.class);
@ -1561,20 +1564,7 @@ public class PlaybackService extends Service implements SharedPreferences.OnShar
private CastConsumer castConsumer = new CastConsumerImpl() { private CastConsumer castConsumer = new CastConsumerImpl() {
@Override @Override
public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) { public void onApplicationConnected(ApplicationMetadata appMetadata, String sessionId, boolean wasLaunched) {
Log.d(TAG, "A cast device application was connected"); PlaybackService.this.onCastAppConnected(wasLaunched);
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();
} }
@Override @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, private void switchMediaPlayer(@NonNull PlaybackServiceMediaPlayer newPlayer,
@NonNull PlaybackServiceMediaPlayer.PSMPInfo info) { @NonNull PlaybackServiceMediaPlayer.PSMPInfo info) {
if (mediaPlayer != null) { if (mediaPlayer != null) {