Media Session Compat未在Pre-Lollipop上显示锁屏控件 [英] Media Session Compat not showing Lockscreen controls on Pre-Lollipop
问题描述
我正在使用AppCompat支持库修订版22中的MediaSessionCompat
.在Lollipop上,我会收到通知&锁屏的背景也是专辑封面.一切都很棒.
I'm using MediaSessionCompat
from AppCompat Support Library Revision 22. And on Lollipop I'm getting notification & also the background of lockscreen is the album art. And everything works cool.
在Pre-Lollipop设备上,锁定屏幕上的音乐控件完全不显示.这很奇怪我尝试了所有操作,但没有显示,甚至没有背景变化.
While on Pre-Lollipop devices, the music controls on lockscreen are not at all shown. It's weird & I tried everything but it doesn't show up, not even the background changes.
我希望有人可以解决此问题.
I hope someone has solution to this issue.
注意:RemoteControlClient
曾经在Lollipop&奇巧
Note: RemoteControlClient
used to work on Lollipop & KitKat
/**
* Initializes the remote control client
*/
private void setupMediaSession() {
/* Activate Audio Manager */
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
ComponentName mRemoteControlResponder = new ComponentName(getPackageName(),
MediaButtonReceiver.class.getName());
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(mRemoteControlResponder);
mMediaSessionCompat = new MediaSessionCompat(getApplication(), "JairSession", mRemoteControlResponder, null);
mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_SEEK_TO |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_STOP
)
.setState(
isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED,
getCurrentPosition(),
1.0f)
.build();
mMediaSessionCompat.setPlaybackState(playbackStateCompat);
mMediaSessionCompat.setCallback(mMediaSessionCallback);
mMediaSessionCompat.setSessionActivity(retrievePlaybackActions(5));
mMediaSessionCompat.setActive(true);
updateMediaSessionMetaData();
mTransportController = mMediaSessionCompat.getController().getTransportControls();
这是updateMediaSessionMetaData()
:
/**
* Updates the lockscreen controls, if enabled.
*/
private void updateMediaSessionMetaData() {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName());
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName());
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName());
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getDuration());
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, MusicUtils.getArtwork(this, getAlbumID(), true));
mMediaSessionCompat.setMetadata(builder.build());
}
媒体会话回调方法
private final MediaSessionCompat.Callback mMediaSessionCallback = new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
final String intentAction = mediaButtonEvent.getAction();
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
if (PrefUtils.isHeadsetPause(getBaseContext())) {
Log.d(LOG_TAG, "Headset disconnected");
pause();
}
} else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
final KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) return super.onMediaButtonEvent(mediaButtonEvent);
final int keycode = event.getKeyCode();
final int action = event.getAction();
final long eventTime = event.getEventTime();
if (event.getRepeatCount() == 0 && action == KeyEvent.ACTION_DOWN) {
switch (keycode) {
case KeyEvent.KEYCODE_HEADSETHOOK:
if (eventTime - mLastClickTime < DOUBLE_CLICK) {
playNext(mSongNumber);
mLastClickTime = 0;
} else {
if (isPlaying())
pause();
else resume();
mLastClickTime = eventTime;
}
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
mTransportController.stop();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
if (isMediaPlayerActive()) {
if (isPlaying()) mTransportController.pause();
else mTransportController.play();
}
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
mTransportController.skipToNext();
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
mTransportController.skipToPrevious();
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
mTransportController.pause();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
mTransportController.play();
break;
}
}
}
return super.onMediaButtonEvent(mediaButtonEvent);
}
@Override
public void onPlay() {
super.onPlay();
resume();
}
@Override
public void onPause() {
super.onPause();
pause();
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
playNext(mSongNumber);
}
@Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
playPrevious(mSongNumber);
}
@Override
public void onSeekTo(long pos) {
super.onSeekTo(pos);
seekTo(pos);
}
@Override
public void onStop() {
super.onStop();
pause();
commitMusicData();
updatePlayingUI(STOP_ACTION);
stopSelf();
}
};
媒体按钮接收器清单条目
Media Button Receiver Manifest Entry
<!-- Media button receiver -->
<receiver android:name=".receiver.MediaButtonReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
</receiver>
几周以来,我一直在努力解决该问题,但没有成功,而且迫切需要帮助.
I'm trying since couple of weeks to solve this issue with no success, and in desperate need of help.
也可以使用MediaSessionCompat的教程或示例
A tutorial or example of MediaSessionCompat would also be fine
推荐答案
最后,我找到了解决方案.感谢 @ianhanniballake & @ user1549672
Finally I figured a solution for this. Thanks to @ianhanniballake & @user1549672
- 按照音频焦点 ="https://stackoverflow.com/users/1676363/ianhanniballake"> @ianhanniballake 如果在Google&上搜索到
- 添加音乐意图
BroadcastReceiver
,则可以找到.也在官方Android文档上 - 写下上面我的问题中给出的
setupMediaSession()
- 非常重要正确地提及了
Flags
- 写
MediaSessionCallbacks
也在上方 - 非常重要更新
MediaPlayer#onPause()
,MediaPlayer#onStart()
和MediaSession
上的MediaSession
最后,当播放由 @ user1549672 提到的新歌曲(还包括下一首和上一首歌曲)时
- 释放
onDestory()
中的
MediaSession
对象
- Add Audio Focus as suggested by @ianhanniballake
- Add Music Intent
BroadcastReceiver
can be found if searched on Google & also on Official Android Docs - Write the
setupMediaSession()
given in my question above - VERY IMPORTANT Mention the
Flags
properly - Write
MediaSessionCallbacks
also available above - VERY IMPORTANT Update the
MediaSession
onMediaPlayer#onPause()
,MediaPlayer#onStart()
& finally when new song is played (also includes next & previous played) mentioned by @user1549672 - Release the
MediaSession
object inonDestory()
仅此而已,大部分材料(代码)都在上面.这个问题花了几个月的时间解决,终于解决了.
Well that's all, most of the material (code) is available above. This question took couple of months to solve, finally it's done.
这篇关于Media Session Compat未在Pre-Lollipop上显示锁屏控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!