GitHub地址:github.com/lizixian18/…java
在平常开发中,若是项目中须要添加音频播放功能,是一件很麻烦的事情。通常须要处理的事情大概有音频服务的封装,播放器的封装,通知栏管理,联动系统媒体中心,音频焦点的获取,播放列表维护,各类API方法的编写等等...若是完善一点,还须要用到IPC去实现。 可见须要处理的事情很是多。android
因此 MusicLibrary 就这样编写出来了,它的目标是帮你所有实现好因此音频相关的事情,让你能够专一于其余事情。git
为体现 MusicLibrary 在实际上的应用,编写了一个简单的音乐播放器 NiceMusic。github
GitHub地址:github.com/lizixian18/…算法
MusicLibrary 的基本用法,能够参考这个项目中的实现。
在 NiceMusic 中,你能够学到下面的东西:数组
关于 IPC 和 AIDL 等用法和原理再也不讲,若是不了解请本身查阅资料。
能够看到,PlayControl
实际上是一个Binder
,链接着客户端和服务端。框架
QueueManager
是播放列表管理类,里面维护着当前的播放列表和当前的音频索引。
播放列表存储在一个 ArrayList 里面,音频索引默认是 0:布局
public QueueManager(MetadataUpdateListener listener, PlayMode playMode) {
mPlayingQueue = Collections.synchronizedList(new ArrayList<SongInfo>());
mCurrentIndex = 0;
...
}
复制代码
当调用设置播放列表相关API的时候,其实是调用了里面的setCurrentQueue
方法,每次播放列表都会先清空,再赋值:ui
public void setCurrentQueue(List<SongInfo> newQueue, int currentIndex) {
int index = 0;
if (currentIndex != -1) {
index = currentIndex;
}
mCurrentIndex = Math.max(index, 0);
mPlayingQueue.clear();
mPlayingQueue.addAll(newQueue);
//通知播放列表更新了
List<MediaSessionCompat.QueueItem> queueItems = QueueHelper.getQueueItems(mPlayingQueue);
if (mListener != null) {
mListener.onQueueUpdated(queueItems, mPlayingQueue);
}
}
复制代码
当播放列表更新后,会把播放列表封装成一个 QueueItem 列表回调给 MediaSessionManager 作锁屏的时候媒体相关的操做。
获得当前播放音乐,播放指定音乐等操做其实是操做音频索引mCurrentIndex
,而后根据索引取出列表中对应的音频信息。
上一首下一首等操做,其实是调用了skipQueuePosition
方法,这个方法中采用了取余的算法来计算上一首下一首的索引,
而不是加一或者减一,这样的一个好处是避免了数组越界或者说计算更方便:
public boolean skipQueuePosition(int amount) {
int index = mCurrentIndex + amount;
if (index < 0) {
// 在第一首歌曲以前向后跳,让你在第一首歌曲上
index = 0;
} else {
//当在最后一首歌时点下一首将返回第一首个
index %= mPlayingQueue.size();
}
if (!QueueHelper.isIndexPlayable(index, mPlayingQueue)) {
return false;
}
mCurrentIndex = index;
return true;
}
复制代码
参数 amount 是维度的意思,能够看到,传 1 则会取下一首,传 -1 则会取上一首,事实上能够取到任何一首音频,
只要维度不同就能够。
播放音乐时,先是调用了setCurrentQueueIndex
方法设置好音频索引后再经过回调交给PlaybackManager
去作真正的播放处理。
private void setCurrentQueueIndex(int index, boolean isJustPlay, boolean isSwitchMusic) {
if (index >= 0 && index < mPlayingQueue.size()) {
mCurrentIndex = index;
if (mListener != null) {
mListener.onCurrentQueueIndexUpdated(mCurrentIndex, isJustPlay, isSwitchMusic);
}
}
}
复制代码
QueueManager 须要说明的感受就这些,其余若是有兴趣能够clone代码后再具体细看。
PlaybackManager 是播放管理类,负责操做播放,暂停等播放控制操做。
它实现了 Playback.Callback 接口,而 Playback 是定义了播放器相关操做的接口。
具体的播放器 ExoPlayer、MediaPlayer 的实现均实现了 Playback 接口,而 PlaybackManager 则是经过 Playback
来统一管理播放器的相关操做。 因此,若是想再添加一个播放器,只须要实现 Playback 接口便可。
播放:
public void handlePlayRequest() {
SongInfo currentMusic = mQueueManager.getCurrentMusic();
if (currentMusic != null) {
String mediaId = currentMusic.getSongId();
boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
if (mediaHasChanged) {
mCurrentMediaId = mediaId;
notifyPlaybackSwitch(currentMusic);
}
//播放
mPlayback.play(currentMusic);
//更新媒体信息
mQueueManager.updateMetadata();
updatePlaybackState(null);
}
}
复制代码
播放方法有几个步骤:
mPlayback.play(currentMusic)
交给具体播放器去播放。暂停:
public void handlePauseRequest() {
if (mPlayback.isPlaying()) {
mPlayback.pause();
updatePlaybackState(null);
}
}
复制代码
暂停是直接交给具体播放器去暂停,而后回调播放状态状态。
中止:
public void handleStopRequest(String withError) {
mPlayback.stop(true);
updatePlaybackState(withError);
}
复制代码
中止也是一样道理。
基本上PlaybackManager
里面的操做都是围绕着这三个方法进行,其余则是一些封装和回调的处理。
具体的播放器实现参考的是Google的官方例子 android-UniversalMusicPlayer 这项目真的很是不错。
这个类主要是管理媒体信息MediaSessionCompat
,他的写法是比较固定的,能够参考这篇文章中的联动系统媒体中心 的介绍
也能够参考 Google的官方例子
这个类是封装了通知栏的相关操做。自定义通知栏的状况可算是很是复杂了,远不止是 new 一个 Notification。(可能我仍是菜鸟)
NotificationCompat.Builder 里面 setContentView
的方法一共有两个,一个是 setCustomContentView()
一个是 setCustomBigContentView()
可知道区别就是大小的区别吧,对应的 RemoteView 也是两个:RemoteView 和 BigRemoteView
而不一样的手机,有的通知栏背景是白色的,有的是透明或者黑色的(如魅族,小米等),这时候你就须要根据不一样的背景显示不一样的样式(除非你在布局里面写死背景色,可是那样真的很丑)
因此通知栏总共须要的布局有四个:
设置 ContentView 以下所示:
...
if (Build.VERSION.SDK_INT >= 24) {
notificationBuilder.setCustomContentView(mRemoteView);
if (mBigRemoteView != null) {
notificationBuilder.setCustomBigContentView(mBigRemoteView);
}
}
...
Notification notification;
if (Build.VERSION.SDK_INT >= 16) {
notification = notificationBuilder.build();
} else {
notification = notificationBuilder.getNotification();
}
if (Build.VERSION.SDK_INT < 24) {
notification.contentView = mRemoteView;
if (Build.VERSION.SDK_INT >= 16 && mBigRemoteView != null) {
notification.bigContentView = mBigRemoteView;
}
}
...
复制代码
在配置通知栏的时候,最重要的就是如何获取到对应的资源文件和布局里面相关的控件,是经过 Resources#getIdentifier
方法去获取:
private Resources res;
private String packageName;
public MediaNotificationManager(){
packageName = mService.getApplicationContext().getPackageName();
res = mService.getApplicationContext().getResources();
}
private int getResourceId(String name, String className) {
return res.getIdentifier(name, className, packageName);
}
复制代码
由于须要能动态配置,因此对通知栏的相关资源和id等命名就须要制定好约定了。好比我要获取
白色背景下ContentView的布局文件赋值给RemoteView:
RemoteViews remoteView = new RemoteViews(packageName,
getResourceId("view_notify_light_play", "layout"));
复制代码
只要你的布局文件命名为 view_notify_light_play.xml
就能正确获取了。
因此不一样的布局和不一样的资源获取所有都是经过 getResourceId
方法获取。
更新UI分为下面3个步骤:
更新开始播放的时候播放/暂停按钮UI:
public void updateViewStateAtStart() {
if (mNotification != null) {
boolean isDark = NotificationColorUtils.isDarkNotificationBar(mService);
mRemoteView = createRemoteViews(isDark, false);
mBigRemoteView = createRemoteViews(isDark, true);
if (Build.VERSION.SDK_INT >= 16) {
mNotification.bigContentView = mBigRemoteView;
}
mNotification.contentView = mRemoteView;
if (mRemoteView != null) {
mRemoteView.setImageViewResource(getResourceId(ID_IMG_NOTIFY_PLAY_OR_PAUSE, "id"),
getResourceId(isDark ? DRAWABLE_NOTIFY_BTN_DARK_PAUSE_SELECTOR :
DRAWABLE_NOTIFY_BTN_LIGHT_PAUSE_SELECTOR, "drawable"));
if (mBigRemoteView != null) {
mBigRemoteView.setImageViewResource(getResourceId(ID_IMG_NOTIFY_PLAY_OR_PAUSE, "id"),
getResourceId(isDark ? DRAWABLE_NOTIFY_BTN_DARK_PAUSE_SELECTOR :
DRAWABLE_NOTIFY_BTN_LIGHT_PAUSE_SELECTOR, "drawable"));
}
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
}
}
复制代码
点击事件经过的就是 RemoteView.setOnClickPendingIntent(PendingIntent pendingIntent)
方法去实现的。
若是可以动态配置,关键就是配置 PendingIntent
就能够了。
思路就是:若是外部有传PendingIntent
进来,就用传进来的PendingIntent
,不然就用默认的PendingIntent
。
private PendingIntent startOrPauseIntent;
public MediaNotificationManager(){
setStartOrPausePendingIntent(creater.getStartOrPauseIntent());
}
private RemoteViews createRemoteViews(){
if (startOrPauseIntent != null) {
remoteView.setOnClickPendingIntent(getResourceId(ID_IMG_NOTIFY_PLAY_OR_PAUSE, "id"),
startOrPauseIntent);
}
}
private void setStartOrPausePendingIntent(PendingIntent pendingIntent) {
startOrPauseIntent = pendingIntent == null ? getPendingIntent(ACTION_PLAY_PAUSE) : pendingIntent;
}
private PendingIntent getPendingIntent(String action) {
Intent intent = new Intent(action);
intent.setClass(mService, PlayerReceiver.class);
return PendingIntent.getBroadcast(mService, 0, intent, 0);
}
复制代码
能够看到,完整代码如上所示,当 creater.getStartOrPauseIntent()
不为空时,就用 creater.getStartOrPauseIntent()
不然用默认的。
但愿你们喜欢! ^_^