下面列出了怎么用android.media.RemoteControlClient的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressLint("NewApi")
@Override
public void onDestroy() {
super.onDestroy();
if (remoteControlClient != null) {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.clear();
metadataEditor.apply();
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession.release();
}
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
}
}
@SuppressLint("NewApi")
@Override
public void onDestroy() {
super.onDestroy();
if (remoteControlClient != null) {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.clear();
metadataEditor.apply();
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession.release();
}
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void registerRemoteControl(ComponentName rcvMedia) {
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(rcvMedia);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, mediaButtonIntent, 0);
remoteControlClient = new RemoteControlClient(mediaPendingIntent);
remoteControlClient.setTransportControlFlags(
RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE |
RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
);
mAudioManager.registerRemoteControlClient(remoteControlClient);
}
public void showNotify(String artist, String album, String title, int tracknr) {
getAudioFocus();
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
MetadataEditor edit = mRemoteControlClient.editMetadata(true);
edit.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title);
edit.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist);
edit.putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, artist);
edit.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album);
edit.putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, tracknr);
edit.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, 10);
edit.apply();
int timeout = getTimeout();
if (timeout != 0) {
mHandler.removeMessages(HANDLER_WHAT_CLEAR);
mHandler.sendEmptyMessageDelayed(HANDLER_WHAT_CLEAR, timeout * 1000);
}
}
/**
* Propagate the playback state to the media session and the lock screen remote control.
* <p/>
* See also :
* {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_STOPPED}
* {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_PLAYING}
* {@link fr.tvbarthel.cheerleader.library.media.MediaSessionWrapper#PLAYBACK_STATE_PAUSED}
*
* @param state playback state.
*/
@SuppressWarnings("deprecation")
public void setPlaybackState(int state) {
switch (state) {
case PLAYBACK_STATE_STOPPED:
setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_STOPPED);
mMediaSession.setActive(false);
break;
case PLAYBACK_STATE_PLAYING:
setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_PLAYING);
mMediaSession.setActive(true);
break;
case PLAYBACK_STATE_PAUSED:
setRemoteControlClientPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_PAUSED);
break;
default:
Log.e(TAG, "Unknown playback state.");
break;
}
}
/**
* A function to control the Remote Control Client. It is needed for
* compatibility with devices below Ice Cream Sandwich (4.0).
*
* @param state Playback state
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setRemoteControlClientPlaybackState(int state) {
if (!AndroidUtil.isICSOrLater() || mRemoteControlClient == null)
return;
switch (state) {
case MediaPlayer.Event.Playing:
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
break;
case MediaPlayer.Event.Paused:
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
break;
case MediaPlayer.Event.Stopped:
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
break;
}
}
@SuppressLint("NewApi")
@Override
public void onDestroy() {
unregisterReceiver(headsetPlugReceiver);
super.onDestroy();
if (remoteControlClient != null) {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.clear();
metadataEditor.apply();
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession.release();
}
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.httpFileDidLoad);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.fileDidLoad);
}
}
/**
* A map between {@link android.media.RemoteControlClient} flags and {@link android.media.session.PlaybackState} actions.
* @return The value to provide for {@link android.media.session.PlaybackState} for actions.
*/
private static long getPlaybackStateActions(final int transportControlFlags) {
final Map<Integer, Long> FLAG_MAP = new HashMap<>();
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_STOP, PlaybackStateCompat.ACTION_STOP);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_NEXT, PlaybackStateCompat.ACTION_SKIP_TO_NEXT);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PAUSE, PlaybackStateCompat.ACTION_PAUSE);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD, PlaybackStateCompat.ACTION_FAST_FORWARD);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_REWIND, PlaybackStateCompat.ACTION_REWIND);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY, PlaybackStateCompat.ACTION_PLAY);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE, PlaybackStateCompat.ACTION_PLAY_PAUSE);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_RATING, PlaybackStateCompat.ACTION_SET_RATING);
long actions = 0;
for (Map.Entry<Integer, Long> flags : FLAG_MAP.entrySet()) {
if ((transportControlFlags & flags.getKey()) == flags.getKey()) {
if (actions == 0)
actions = flags.getValue();
else
actions |= flags.getValue();
}
}
return actions;
}
static int getStateFromPlayState(PlayState playState) {
switch (playState) {
case BUFFERING:
return RemoteControlClient.PLAYSTATE_BUFFERING;
case ERROR:
return RemoteControlClient.PLAYSTATE_ERROR;
case FAST_FORWARDING:
return RemoteControlClient.PLAYSTATE_FAST_FORWARDING;
case PAUSED:
return RemoteControlClient.PLAYSTATE_PAUSED;
case PLAYING:
return RemoteControlClient.PLAYSTATE_PLAYING;
case REWINDING:
return RemoteControlClient.PLAYSTATE_REWINDING;
case SKIPPING_BACKWARDS:
return RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS;
case SKIPPING_FORWARDS:
return RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS;
case STOPPED:
return RemoteControlClient.PLAYSTATE_STOPPED;
default:
return RemoteControlClient.PLAYSTATE_ERROR;
}
}
public boolean wasPlayingRecently() {
switch (mState) {
case RemoteControlClient.PLAYSTATE_PLAYING:
case RemoteControlClient.PLAYSTATE_FAST_FORWARDING:
case RemoteControlClient.PLAYSTATE_REWINDING:
case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS:
case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS:
case RemoteControlClient.PLAYSTATE_BUFFERING:
// actively playing or about to play
return true;
case RemoteControlClient.PLAYSTATE_STOPPED:
case RemoteControlClient.PLAYSTATE_PAUSED:
case RemoteControlClient.PLAYSTATE_ERROR:
return ((SystemClock.elapsedRealtime() - mStateChangeTimeMs) < DISPLAY_TIMEOUT_MS);
default:
LOGE("PlaybackInfo", "Unknown playback state " + mState + " in wasPlayingRecently()");
return false;
}
}
public void a(boolean flag, long l1, int i1)
{
if (m != null)
{
RemoteControlClient remotecontrolclient = m;
byte byte0;
float f1;
if (flag)
{
byte0 = 3;
} else
{
byte0 = 1;
}
if (flag)
{
f1 = 1.0F;
} else
{
f1 = 0.0F;
}
remotecontrolclient.setPlaybackState(byte0, l1, f1);
m.setTransportControlFlags(i1);
}
}
public void register(final Context context, final ComponentName mediaButtonReceiverComponent) {
downloadService = (DownloadService) context;
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
// build the PendingIntent for the remote control client
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0);
// create and register the remote control client
mRemoteControl = new RemoteControlClient(mediaPendingIntent);
audioManager.registerRemoteControlClient(mRemoteControl);
mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
mRemoteControl.setTransportControlFlags(getTransportFlags());
imageLoader = SubsonicActivity.getStaticImageLoader(context);
}
public void updateMetadata(final Context context, final MusicDirectory.Entry currentSong) {
if(mRemoteControl == null) {
return;
}
if(imageLoader == null) {
imageLoader = SubsonicActivity.getStaticImageLoader(context);
}
// Update the remote controls
RemoteControlClient.MetadataEditor editor = mRemoteControl.editMetadata(true);
updateMetadata(currentSong, editor);
editor.apply();
if (currentSong == null || imageLoader == null) {
updateAlbumArt(currentSong, null);
} else {
imageLoader.loadImage(context, this, currentSong);
}
}
@SuppressLint("NewApi")
private RemoteControlClient setupRemoteControlClient() {
if (Build.VERSION.SDK_INT < 14) {
return null;
}
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(new ComponentName(getPackageName(),
MediaButtonReceiver.class.getName()));
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, mediaButtonIntent, 0);
remoteControlClient = new RemoteControlClient(mediaPendingIntent);
int controlFlags;
if (android.os.Build.VERSION.SDK_INT < 16) {
controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT;
} else {
controlFlags = RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE;
}
remoteControlClient.setTransportControlFlags(controlFlags);
return remoteControlClient;
}
/**
* A map between {@link android.media.RemoteControlClient} flags and {@link android.media.session.PlaybackState} actions.
* @return The value to provide for {@link android.media.session.PlaybackState} for actions.
*/
private static long getPlaybackStateActions(final int transportControlFlags) {
final Map<Integer, Long> FLAG_MAP = new HashMap<>();
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_STOP, PlaybackStateCompat.ACTION_STOP);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_NEXT, PlaybackStateCompat.ACTION_SKIP_TO_NEXT);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PAUSE, PlaybackStateCompat.ACTION_PAUSE);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD, PlaybackStateCompat.ACTION_FAST_FORWARD);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_REWIND, PlaybackStateCompat.ACTION_REWIND);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY, PlaybackStateCompat.ACTION_PLAY);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE, PlaybackStateCompat.ACTION_PLAY_PAUSE);
FLAG_MAP.put(RemoteControlClient.FLAG_KEY_MEDIA_RATING, PlaybackStateCompat.ACTION_SET_RATING);
long actions = 0;
for (Map.Entry<Integer, Long> flags : FLAG_MAP.entrySet()) {
if ((transportControlFlags & flags.getKey()) == flags.getKey()) {
if (actions == 0)
actions = flags.getValue();
else
actions |= flags.getValue();
}
}
return actions;
}
static int getStateFromPlayState(PlayState playState) {
switch (playState) {
case BUFFERING:
return RemoteControlClient.PLAYSTATE_BUFFERING;
case ERROR:
return RemoteControlClient.PLAYSTATE_ERROR;
case FAST_FORWARDING:
return RemoteControlClient.PLAYSTATE_FAST_FORWARDING;
case PAUSED:
return RemoteControlClient.PLAYSTATE_PAUSED;
case PLAYING:
return RemoteControlClient.PLAYSTATE_PLAYING;
case REWINDING:
return RemoteControlClient.PLAYSTATE_REWINDING;
case SKIPPING_BACKWARDS:
return RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS;
case SKIPPING_FORWARDS:
return RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS;
case STOPPED:
return RemoteControlClient.PLAYSTATE_STOPPED;
default:
return RemoteControlClient.PLAYSTATE_ERROR;
}
}
public boolean wasPlayingRecently() {
switch (mState) {
case RemoteControlClient.PLAYSTATE_PLAYING:
case RemoteControlClient.PLAYSTATE_FAST_FORWARDING:
case RemoteControlClient.PLAYSTATE_REWINDING:
case RemoteControlClient.PLAYSTATE_SKIPPING_FORWARDS:
case RemoteControlClient.PLAYSTATE_SKIPPING_BACKWARDS:
case RemoteControlClient.PLAYSTATE_BUFFERING:
// actively playing or about to play
return true;
case RemoteControlClient.PLAYSTATE_STOPPED:
case RemoteControlClient.PLAYSTATE_PAUSED:
case RemoteControlClient.PLAYSTATE_ERROR:
return ((SystemClock.elapsedRealtime() - mStateChangeTimeMs) < DISPLAY_TIMEOUT_MS);
default:
LOGE("PlaybackInfo", "Unknown playback state " + mState + " in wasPlayingRecently()");
return false;
}
}
private void updateButtons() {
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute();
// show pause or resume icon depending on current state
mPauseResumeButton.setImageResource(
mPaused ? R.drawable.ic_action_play : R.drawable.ic_action_pause);
// disable pause/resume/stop if no session
mPauseResumeButton.setEnabled(mSessionManager.hasSession());
mStopButton.setEnabled(mSessionManager.hasSession());
// only enable seek bar when duration is known
PlaylistItem item = getCheckedPlaylistItem();
mSeekBar.setEnabled(item != null && item.getDuration() > 0);
if (mRemoteControlClient != null) {
mRemoteControlClient.setPlaybackState(mPaused ? RemoteControlClient.PLAYSTATE_PAUSED :
RemoteControlClient.PLAYSTATE_PLAYING);
}
}
@SuppressLint("NewApi")
@Override
public void onDestroy() {
unregisterReceiver(headsetPlugReceiver);
super.onDestroy();
if (remoteControlClient != null) {
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.clear();
metadataEditor.apply();
audioManager.unregisterRemoteControlClient(remoteControlClient);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession.release();
}
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.httpFileDidLoad);
NotificationCenter.getInstance(a).removeObserver(this, NotificationCenter.fileDidLoad);
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void registerRemoteControl(ComponentName rcvMedia) {
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(rcvMedia);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, mediaButtonIntent, 0);
remoteControlClient = new RemoteControlClient(mediaPendingIntent);
remoteControlClient.setTransportControlFlags(
RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE |
RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
);
mAudioManager.registerRemoteControlClient(remoteControlClient);
}
private void registerRemoteControlClient() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Create the RCC and register with AudioManager and MediaRouter
mAudioManager.requestAudioFocus(mAfChangeListener, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
mAudioManager.registerMediaButtonEventReceiver(mEventReceiver);
mRemoteControlClient = new RemoteControlClient(mMediaPendingIntent);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
mMediaRouter.addRemoteControlClient(mRemoteControlClient);
SampleMediaButtonReceiver.setActivity(MainActivity.this);
mRemoteControlClient.setTransportControlFlags(RemoteControlClient
.FLAG_KEY_MEDIA_PLAY_PAUSE);
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
}
}
public void startPlaying() {
if (mPlayState != RemoteControlClient.PLAYSTATE_PLAYING) {
mPlayState = RemoteControlClient.PLAYSTATE_PLAYING;
mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
}
if (mFocused) {
takeAudioFocus();
}
}
void gainFocus() {
if (!mFocused) {
mFocused = true;
mAudioManager.registerMediaButtonEventReceiver(mPendingIntent);
mAudioManager.registerRemoteControlClient(mRemoteControl);
if (mPlayState == RemoteControlClient.PLAYSTATE_PLAYING) {
takeAudioFocus();
}
}
}
public void pause() {
BASS.BASS_ChannelPause(stream);
stopForeground(false);
stopUpdateProgress();
saveEqualizerValues();
// Notify playerInterface
onUpdatePlayPause();
// Tell any remote controls that our playback state is 'paused'.
if (remoteControlClient != null) {
updateRemoteControlState(RemoteControlClient.PLAYSTATE_PAUSED);
}
}
public void stop() {
BASS.BASS_ChannelStop(stream);
stopUpdateProgress();
saveEqualizerValues();
onUpdatePlayPause();
if (remoteControlClient != null) {
updateRemoteControlState(RemoteControlClient.PLAYSTATE_STOPPED);
}
}
public void playFromPause() {
BASS.BASS_ChannelPlay(stream, false);
startUpdateProgress();
// Notify playerInterface
onUpdatePlayPause();
if (remoteControlClient != null)
updateRemoteControlState(RemoteControlClient.PLAYSTATE_PLAYING);
updateWidgets();
fireNotification();
}
public void stopPlaying() {
if (mPlayState != RemoteControlClient.PLAYSTATE_STOPPED) {
mPlayState = RemoteControlClient.PLAYSTATE_STOPPED;
mRemoteControl.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
}
dropAudioFocus();
}
@SuppressLint("InlinedApi")
private void setUpRemoteControl(final MediaInfo info) {
if (!isFeatureEnabled(BaseCastManager.FEATURE_LOCKSCREEN)) {
return;
}
LOGD(TAG, "setupRemoteControl() was called");
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
ComponentName eventReceiver = new ComponentName(
mContext, VideoIntentReceiver.class.getName());
mAudioManager.registerMediaButtonEventReceiver(eventReceiver);
if (mRemoteControlClientCompat == null) {
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.setComponent(mMediaButtonReceiverComponent);
mRemoteControlClientCompat = new RemoteControlClientCompat(
PendingIntent.getBroadcast(mContext, 0, intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,
mRemoteControlClientCompat);
}
mRemoteControlClientCompat.addToMediaRouter(mMediaRouter);
mRemoteControlClientCompat.setTransportControlFlags(
RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
if (null == info) {
mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
return;
} else {
mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
}
// Update the remote control's image
updateLockScreenImage(info);
// update the remote control's metadata
updateLockScreenMetadata();
}
/**
* Should be called to released the internal component.
*/
@SuppressWarnings("deprecation")
public void onDestroy() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
mAudioManager.unregisterMediaButtonEventReceiver(mMediaButtonReceiverComponent);
mLocalBroadcastManager.unregisterReceiver(mLockScreenReceiver);
}
mMediaSession.release();
}
/**
* Update meta data used by the remote control client and the media session.
*
* @param track track currently played.
* @param artwork track artwork.
*/
@SuppressWarnings("deprecation")
public void setMetaData(SoundCloudTrack track, Bitmap artwork) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// set meta data on the lock screen for pre lollipop.
mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
RemoteControlClientCompat.MetadataEditorCompat mediaEditorCompat
= mRemoteControlClientCompat.editMetadata(true)
.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, track.getArtist());
if (artwork != null) {
mediaEditorCompat.putBitmap(
RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, artwork);
}
mediaEditorCompat.apply();
}
// set meta data to the media session.
MediaMetadataCompat.Builder metadataCompatBuilder = new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, track.getArtist());
if (artwork != null) {
metadataCompatBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, artwork);
}
mMediaSession.setMetadata(metadataCompatBuilder.build());
setMediaSessionCompatPlaybackState(PlaybackStateCompat.STATE_PLAYING);
}