Implement some of the features present in Google Play Music: (#804)
report the current song's position in the queue to the remote control; report the current song's playback position to the remote control.
This commit is contained in:
1 parent
90325aaf36
commit
3f0c7bb7a3
3 files changed
+34
-10
No files matched your search
@@ -1559,6 +1559,9 @@ public final class PlaybackService extends Service
|
||||
* Otherwise, calls {@link PlaybackService#setCurrentSong(int)} with arg1.
|
||||
*/
|
||||
private static final int MSG_CALL_GO = 8;
|
||||
/**
|
||||
* The combination of (current song, current playback state) changed.
|
||||
*/
|
||||
private static final int MSG_BROADCAST_CHANGE = 10;
|
||||
private static final int MSG_SAVE_STATE = 12;
|
||||
private static final int MSG_PROCESS_SONG = 13;
|
||||
@@ -1567,6 +1570,10 @@ public final class PlaybackService extends Service
|
||||
private static final int MSG_GAPLESS_UPDATE = 16;
|
||||
private static final int MSG_UPDATE_PLAYCOUNTS = 17;
|
||||
private static final int MSG_SHOW_TOAST = 18;
|
||||
/**
|
||||
* The current song's playback position changed.
|
||||
*/
|
||||
private static final int MSG_BROADCAST_SEEK = 19;
|
||||
|
||||
@Override
|
||||
public boolean handleMessage(Message message)
|
||||
@@ -1655,6 +1662,9 @@ public final class PlaybackService extends Service
|
||||
Toast.makeText(this, resId, duration).show();
|
||||
}
|
||||
break;
|
||||
case MSG_BROADCAST_SEEK:
|
||||
mRemoteControlClient.updateRemote(mCurrentSong, mState, mForceNotificationVisible);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -1713,6 +1723,7 @@ public final class PlaybackService extends Service
|
||||
return;
|
||||
long position = (long)mMediaPlayer.getDuration() * progress / 1000;
|
||||
mMediaPlayer.seekTo((int)position);
|
||||
mHandler.sendEmptyMessage(MSG_BROADCAST_SEEK);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1944,6 +1955,7 @@ public final class PlaybackService extends Service
|
||||
ArrayList<TimelineCallback> list = sCallbacks;
|
||||
for (int i = list.size(); --i != -1; )
|
||||
list.get(i).onPositionInfoChanged();
|
||||
mRemoteControlClient.updateRemote(mCurrentSong, mState, mForceNotificationVisible);
|
||||
}
|
||||
|
||||
private final LibraryObserver mObserver = new LibraryObserver() {
|
||||
|
||||
@@ -37,7 +37,7 @@ public class RemoteControlImplICS implements RemoteControl.Client {
|
||||
/**
|
||||
* Context of this instance
|
||||
*/
|
||||
private Context mContext;
|
||||
private final Context mContext;
|
||||
/**
|
||||
* Used with updateRemote method.
|
||||
*/
|
||||
@@ -59,8 +59,6 @@ public class RemoteControlImplICS implements RemoteControl.Client {
|
||||
|
||||
/**
|
||||
* Perform initialization required for RemoteControlClient.
|
||||
*
|
||||
* @param am The AudioManager service.
|
||||
*/
|
||||
public void initializeRemote() {
|
||||
// make sure there is only one registered remote
|
||||
@@ -113,7 +111,7 @@ public class RemoteControlImplICS implements RemoteControl.Client {
|
||||
|
||||
/**
|
||||
* Update the remote with new metadata.
|
||||
* {@link #registerRemote(Context, AudioManager)} must have been called
|
||||
* {@link #initializeRemote()} must have been called
|
||||
* first.
|
||||
*
|
||||
* @param song The song containing the new metadata.
|
||||
|
||||
@@ -34,7 +34,7 @@ public class RemoteControlImplLp implements RemoteControl.Client {
|
||||
/**
|
||||
* Context of this instance
|
||||
*/
|
||||
private Context mContext;
|
||||
private final Context mContext;
|
||||
/**
|
||||
* Objects MediaSession handle
|
||||
*/
|
||||
@@ -82,6 +82,11 @@ public class RemoteControlImplLp implements RemoteControl.Client {
|
||||
public void onSkipToPrevious() {
|
||||
MediaButtonReceiver.processKey(mContext, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
|
||||
}
|
||||
@Override
|
||||
public void onStop() {
|
||||
// We will behave the same as Google Play Music: for "Stop" we unconditionally Pause instead
|
||||
MediaButtonReceiver.processKey(mContext, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE));
|
||||
}
|
||||
});
|
||||
|
||||
Intent intent = new Intent();
|
||||
@@ -113,7 +118,7 @@ public class RemoteControlImplLp implements RemoteControl.Client {
|
||||
|
||||
/**
|
||||
* Update the remote with new metadata.
|
||||
* {@link #registerRemote()} must have been called
|
||||
* {@link #initializeRemote()} must have been called
|
||||
* first.
|
||||
*
|
||||
* @param song The song containing the new metadata.
|
||||
@@ -132,25 +137,34 @@ public class RemoteControlImplLp implements RemoteControl.Client {
|
||||
mShowCover = settings.getBoolean(PrefKeys.COVER_ON_LOCKSCREEN, PrefDefaults.COVER_ON_LOCKSCREEN) ? 1 : 0;
|
||||
}
|
||||
|
||||
PlaybackService service = PlaybackService.get(mContext);
|
||||
|
||||
if (song != null) {
|
||||
Bitmap bitmap = null;
|
||||
if (mShowCover == 1 && (isPlaying || keepPaused)) {
|
||||
bitmap = song.getCover(mContext);
|
||||
}
|
||||
|
||||
session.setMetadata(new MediaMetadata.Builder()
|
||||
MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder()
|
||||
.putString(MediaMetadata.METADATA_KEY_ARTIST, song.artist)
|
||||
.putString(MediaMetadata.METADATA_KEY_ALBUM, song.album)
|
||||
.putString(MediaMetadata.METADATA_KEY_TITLE, song.title)
|
||||
.putLong(MediaMetadata.METADATA_KEY_DURATION, song.duration)
|
||||
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap)
|
||||
.build());
|
||||
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap);
|
||||
|
||||
// logic copied from FullPlaybackActivity.updateQueuePosition()
|
||||
if (PlaybackService.finishAction(service.getState()) != SongTimeline.FINISH_RANDOM) {
|
||||
metadataBuilder.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, service.getTimelinePosition() + 1);
|
||||
metadataBuilder.putLong(MediaMetadata.METADATA_KEY_NUM_TRACKS, service.getTimelineLength());
|
||||
}
|
||||
|
||||
session.setMetadata(metadataBuilder.build());
|
||||
}
|
||||
|
||||
int playbackState = (isPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED);
|
||||
|
||||
session.setPlaybackState(new PlaybackState.Builder()
|
||||
.setState(playbackState, PlaybackState.PLAYBACK_POSITION_UNKNOWN , 1.0f)
|
||||
.setState(playbackState, service.getPosition(), 1.0f)
|
||||
.setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY_PAUSE |
|
||||
PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
|
||||
.build());
|
||||
|
||||
Reference in new issue
Block a user