这篇文章主要详情在运用该框架时遇到的问题和处理办法
媒体应用架构概览
MediSession框架
官方说明允许与媒体控制器、音量键、媒体按钮和传输控件交互(Android 5.0)
安卓新播放器EXOplayer详情
比较重要的几个方法类都在"com.google.android.exoplayer:extension-mediasession"里面,大体的流程如前文中的MediSession框架图
1、列表顺序播放(300多首音乐):
它官方本身是支持把一个列表数据源仍进去,而后可以选择播放模式进行播放的,切换曲目和播放状态改变时会回调,但是300多个数据量太大了,播放器拼接时会引发卡慢。所以技术方案定为单曲仍给播放器。而后维护一个类控制播放列表。
由于单曲给播放器,并且MediaControllerCompat.Callback不会回掉播放结束的缘故,所以要去捕获下播放器的STATE_ENDED(播放结束)状态。
2、已下载、未下载音乐混合播放遇到复杂的网络环境(wifi、2G、3G、4G、无网):
客户的使用场景比较复杂,可能会在网络状况良好的时候,播放并下载了一首音频,而后再收听的时候网络发生了抖动,一直处于网络差的情况,而后播放器的缓存已经播放完了,这个时候播放器回掉STATE_ERROR,需要去切换数据源,查看当前播放列表能否有下载的音频,有,则判断当前播放的音频能否下载,假如下载了,则播放当前的音频;没下载,则往下查找
3、自己设置通知栏:
val builder = NotificationCompat.Builder(context, NOW_PLAYING_CHANNEL) val mediaStyle = MediaStyle() .setMediaSession(sessionToken) val notification = builder.setContentIntent(controller.sessionActivity) .setCustomContentView(remoteView) //设置普通notification视图 .setCustomBigContentView(remoteBigView)//设置显示bigView的notification视图 .setPriority(NotificationCompat.PRIORITY_MAX)//设置最大优先级 .setSmallIcon(R.drawable.fm_push_logo) .setOngoing(true) .setStyle(mediaStyle) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .build()
该方案兼容性差,在4.4.4的手机上发现通知栏数据显示有问题。
参考其余APP:
var notification = if (Build.VERSION.SDK_INT >= 26) { Notification.Builder(context, NOW_PLAYING_CHANNEL) .setOngoing(true).build() } else { if (Build.VERSION.SDK_INT >= 16) { Notification.Builder(context).setOngoing(true).build() } else { Notification() } } notification.apply { contentIntent = controller.sessionActivity contentView = remoteView if (Build.VERSION.SDK_INT >= 16) { bigContentView = remoteBigView } priority = NotificationCompat.PRIORITY_MAX icon = R.drawable.fm_push_logo }
4、聊一聊 isActive:
/** * Sets if this session is currently active and ready to receive commands. If * set to false your session's controller may not be discoverable. You must * set the session to active before it can start receiving media button * events or transport commands. * <p> * On platforms earlier than * {@link android.os.Build.VERSION_CODES#LOLLIPOP}, * a media button event receiver should be set via the constructor to * receive media button events. * * @param active Whether this session is active or not. */ public void setActive(boolean active) { mImpl.setActive(active); for (OnActiveChangeListener listener : mActiveListeners) { listener.onActiveChanged(); } }
现实情况:文档说设置了session才可被发现,不过设置了原生手机锁屏显示这件事情随缘。