diff options
author | Domingos Lopes <domingos86lopes+github@gmail.com> | 2016-03-27 19:42:33 -0400 |
---|---|---|
committer | Domingos Lopes <domingos86lopes+github@gmail.com> | 2016-04-23 21:39:56 -0400 |
commit | f79369a9ad3fc882326d40a2c64e7192b482a079 (patch) | |
tree | f080056e517259300859eb44f8f4732ec238870a /core | |
parent | 6ee3d20a80f78705655fc6b55aa791dea76d09b0 (diff) | |
download | AntennaPod-f79369a9ad3fc882326d40a2c64e7192b482a079.zip |
finishing handling remote playback updates
Diffstat (limited to 'core')
3 files changed, 58 insertions, 22 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/cast/CastManager.java b/core/src/main/java/de/danoeh/antennapod/core/cast/CastManager.java index 75b5928bf..59a10c89f 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/cast/CastManager.java +++ b/core/src/main/java/de/danoeh/antennapod/core/cast/CastManager.java @@ -121,6 +121,7 @@ public class CastManager extends BaseCastManager implements OnFailedListener { public static synchronized CastManager init(Context context) { if (INSTANCE == null) { + //TODO also setup dialog factory if necessary CastConfiguration castConfiguration = new CastConfiguration.Builder(CAST_APP_ID) .enableDebug() .enableAutoReconnect() diff --git a/core/src/main/java/de/danoeh/antennapod/core/cast/CastUtils.java b/core/src/main/java/de/danoeh/antennapod/core/cast/CastUtils.java index d69256864..f0a7214c9 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/cast/CastUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/cast/CastUtils.java @@ -293,7 +293,8 @@ public class CastUtils { /** * Compares a {@link MediaInfo} instance with a {@link Playable} and evaluates whether they - * represent the same podcast episode. + * represent the same podcast episode. Useful every time we get a MediaInfo from the Cast Device + * and want to avoid unnecessary conversions. * * @param info the {@link MediaInfo} object to be compared. * @param media the {@link Playable} object to be compared. 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 fa9823295..7ed5a2fd9 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 @@ -1,6 +1,7 @@ package de.danoeh.antennapod.core.service.playback; import android.content.Context; +import android.media.MediaPlayer; import android.support.annotation.NonNull; import android.util.Log; import android.util.Pair; @@ -21,6 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import de.danoeh.antennapod.core.cast.CastConsumer; import de.danoeh.antennapod.core.cast.CastConsumerImpl; import de.danoeh.antennapod.core.cast.CastManager; +import de.danoeh.antennapod.core.cast.RemoteMedia; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.MediaType; import de.danoeh.antennapod.core.preferences.UserPreferences; @@ -41,9 +43,9 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { private volatile MediaInfo remoteMedia; private volatile MediaType mediaType; - private volatile PlayerStatus statusBeforeSeeking; + private final AtomicBoolean isBuffering; - private volatile AtomicBoolean startWhenPrepared; + private final AtomicBoolean startWhenPrepared; /** * Some asynchronous calls might change the state of the MediaPlayer object. Therefore calls in other threads @@ -56,10 +58,10 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { super(context, callback); castMgr = CastManager.getInstance(); - statusBeforeSeeking = null; media = null; mediaType = null; - this.startWhenPrepared = new AtomicBoolean(false); + startWhenPrepared = new AtomicBoolean(false); + isBuffering = new AtomicBoolean(false); //playerLock = new ReentrantLock(); executor = new ThreadPoolExecutor(1, 1, 5, TimeUnit.MINUTES, new LinkedBlockingDeque<>(), @@ -80,7 +82,13 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { public void onRemoteMediaPlayerStatusUpdated() { MediaStatus status = castMgr.getMediaStatus(); Playable currentMedia = localVersion(status.getMediaInfo()); - switch (status.getPlayerState()) { + long position = status.getStreamPosition(); + if (position > 0 && currentMedia.getPosition()==0) { + currentMedia.setPosition((int) position); + } + int state = status.getPlayerState(); + setBuffering(state == MediaStatus.PLAYER_STATE_BUFFERING); + switch (state) { case MediaStatus.PLAYER_STATE_PLAYING: setPlayerStatus(PlayerStatus.PLAYING, currentMedia); break; @@ -88,7 +96,7 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { setPlayerStatus(PlayerStatus.PAUSED, currentMedia); break; case MediaStatus.PLAYER_STATE_BUFFERING: - //TODO + setPlayerStatus(playerStatus, currentMedia); break; case MediaStatus.PLAYER_STATE_IDLE: int reason = status.getIdleReason(); @@ -103,24 +111,23 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { setPlayerStatus(PlayerStatus.INITIALIZED, currentMedia); break; case MediaStatus.IDLE_REASON_FINISHED: - //TODO endPlayback and start a new one + boolean playing = playerStatus == PlayerStatus.PLAYING; + setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); + callback.endPlayback(playing, false, false); break; case MediaStatus.IDLE_REASON_ERROR: - //TODO what do they mean by error? Can we easily recover by sending a new command? + //Let's assume it's a media format error. Skipping... + setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); + callback.endPlayback(startWhenPrepared.get(), true, false); } break; case MediaStatus.PLAYER_STATE_UNKNOWN: - //TODO - + //is this right? + setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia); } } @Override - public void onStreamVolumeChanged(double value, boolean isMute) { - //TODO - } - - @Override public void onMediaLoadResult(int statusCode) { if (playerStatus == PlayerStatus.PREPARING) { if (statusCode == CastStatusCodes.SUCCESS) { @@ -143,10 +150,38 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { } }; + private void setBuffering(boolean buffering) { + if (buffering && isBuffering.compareAndSet(false, true)) { + callback.onMediaPlayerInfo(MediaPlayer.MEDIA_INFO_BUFFERING_START); + } else if (!buffering && isBuffering.compareAndSet(true, false)) { + callback.onMediaPlayerInfo(MediaPlayer.MEDIA_INFO_BUFFERING_END); + } + } + private Playable localVersion(MediaInfo info){ - // TODO compare with current media. If it doesn't match, then either find a local version for it - // or create an appropriate one. - return media; + if (info == null) { + return null; + } + if (CastUtils.matches(info, media)) { + return media; + } + return CastUtils.getPlayable(info, true); + } + + private MediaInfo remoteVersion(Playable playable) { + if (playable == null) { + return null; + } + if (CastUtils.matches(remoteMedia, playable)) { + return remoteMedia; + } + if (playable instanceof FeedMedia) { + return CastUtils.convertFromFeedMedia((FeedMedia) playable); + } + if (playable instanceof RemoteMedia) { + return ((RemoteMedia) playable).extractMediaInfo(); + } + return null; } @Override @@ -194,7 +229,7 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { } this.media = playable; - remoteMedia = CastUtils.convertFromFeedMedia((FeedMedia) media); + remoteMedia = remoteVersion(playable); //this.stream = stream; this.mediaType = media.getMediaType(); this.startWhenPrepared.set(startWhenPrepared); @@ -448,10 +483,9 @@ public class RemotePSMP extends PlaybackServiceMediaPlayer { @Override protected void setPlayable(Playable playable) { - //TODO this looks very wrong if (playable != media) { media = playable; - remoteMedia = !(media instanceof FeedMedia) ? null : CastUtils.convertFromFeedMedia((FeedMedia) media); + remoteMedia = remoteVersion(playable); } } |