android music app架构,android-UniversalMusicPlayer 的项目运用

这篇文章主要介绍在运用该框架时遇到的问题和解决办法

简单的知识点介绍:

MediSession框架

官方说明允许与媒体控制器、音量键、媒体按钮和传输控件交互(Android 5.0)

47c20554096b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

流媒体.jpg

实现功能

比较重要的几个方法类都在"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.

*

* 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才可被发现,不过设置了原生手机锁屏显示这件事情随缘。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值