Android5.0 提出了全新的MediaSession概念用于播放器与控制器之间进行交互,它取代之前的RemoteControlClient,并提供了更为灵活的客户端受控端模型。
但是MediaSession框架只能在安卓5系统上使用,如何兼容低版本呢? Google在support-v4(21以上版本)中也提供了MediaSessionCompact兼容包。通过它可以告诉Android系统与其他的应用,自己正在播放的内容是什么以及自己支持哪些类型的播放控制。
在播放音乐时,通过MediaSessionCompat可以实现锁屏按键上的监听,从而转换成自己的操作。
本文介绍的是基于MediaSessionCompat实现锁屏界面进行音乐播放控制功能
package com.yobbom.jiheBoxplayer.service; import android.os.Build; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; import com.yobbom.jiheBoxplayer.model.Music; /** * Created by Administrator on 2019/4/3. */ public class MediaSessionManager { private static final String TAG = "MediaSessionManager"; private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIoUS | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO; private PlayService playService; private MediaSessionCompat mediaSession; public static MediaSessionManager get(){ return SingletonHolder.instance; } private static class SingletonHolder{ private static MediaSessionManager instance = new MediaSessionManager(); } private MediaSessionManager(){ } //音乐的控制逻辑都在PlayService服务中,将service实例传递过来,与MediaSessionManager进行交互 private void init(PlayService playService){ this.playService = playService; setupMediaSession(); } private void setupMediaSession(){ mediaSession = new MediaSessionCompat(playService,TAG); //指明支持的按键信息类型 mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mediaSession.setCallback(callback);//设置监听回调 mediaSession.setActive(true);//必须设置为true,这样才能开始接收各种信息 } //点击控制按钮时,更新播放状态 public void updatePlaybackState(){ int state = (AudioPlayer.get().isPlaying() || AudioPlayer.get().isPreparing()) ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; //第三个参数必须为1,否则锁屏上面显示的时长会有问题 mediaSession.setPlaybackState( //监听的事件(播放,暂停,上一曲,下一曲) new PlaybackStateCompat.Builder() .setActions(MEDIA_SESSION_ACTIONS) .setState(state,AudioPlayer.get().getAudioPosition(),1) .build()); } //播放歌曲时,需要更新屏幕上的歌曲信息 public void updateMetaData(Music music){ if(music == null){ mediaSession.setMetadata(null); return; } //Log.d(TAG,"parseMp3File名称: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_TITLE)); //Log.d(TAG,"parseMp3File专辑: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_ALBUM)); //Log.d(TAG,"parseMp3File歌手: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_ARTIST)); //Log.d(TAG,"parseMp3File码率: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_BITRATE)); //Log.d(TAG,"parseMp3File时长: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_DURATION)); //Log.d(TAG,"parseMp3File类型: " + mmr.extractMetadata(MediaMetadataRetriever.MetaDATA_KEY_MIMETYPE)); MediaMetadataCompat.Builder MetaData = new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.MetaDATA_KEY_TITLE,music.getTitle()) .putString(MediaMetadataCompat.MetaDATA_KEY_ARTIST,music.getArtist()) .putString(MediaMetadataCompat.MetaDATA_KEY_ALBUM,music.getAlbum()) .putString(MediaMetadataCompat.MetaDATA_KEY_ALBUM_ARTIST,music.getArtist()) .putLong(MediaMetadataCompat.MetaDATA_KEY_DURATION,music.getDuration()) .putBitmap(MediaMetadataCompat.MetaDATA_KEY_ALBUM_ART,CoverLoader.get().loadThumb(music)); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ MetaData.putLong(MediaMetadataCompat.MetaDATA_KEY_NUM_TRACKS,AppCache.get().getLocalMusicList().size()); } mediaSession.setMetadata(MetaData.build()); } //初始化回调,用于监听锁屏界面上的按钮事件 private MediaSessionCompat.Callback callback = new MediaSessionCompat.Callback() { @Override public void onPlay() { AudioPlayer.get().playPause(); } @Override public void onPause() { AudioPlayer.get().playPause(); } @Override public void onSkipToNext() { AudioPlayer.get().next(); } @Override public void onSkipToPrevIoUs() { AudioPlayer.get().prev(); } @Override public void onStop() { AudioPlayer.get().stopPlayer(); } @Override public void onSeekTo(long pos) { AudioPlayer.get().seekTo((int)pos); } }; }原文链接:https://www.f2er.com/android/534534.html