上一篇简要的看了看MusicPlayerActivity持有的MediaBrowser和MidiaController两个对象。
接下来,跟踪MusicPlayerActivity的生命周期,来了解程序的运行过程。(因为MusicPlayerActivity继承BaseActivity,在MusicPlayerActivity的生命周期函数中,默认执行了BaseActivity的生命周期函数)。
BaseActivity的onCreate()函数中的部分代码:
mMediaBrowser = new MediaBrowserCompat(this,
new ComponentName(this, MusicService.class), mConnectionCallback, null);
public MediaBrowserCompat(Context context, ComponentName serviceComponent,ConnectionCallback callback, Bundle rootHints)
创建了在前面的提到的MediaBrowserCompat对象。
BaseActivity的onStart()函数中的部分代码:
mMediaBrowser.connect();
点进去看它的实现:
try {
bound = mContext.bindService(intent,mServiceConnection, Context.BIND_AUTO_CREATE);
} catch (Exception ex) {
Log.e(TAG, "Failed binding to service " + mServiceComponent);
}
到这儿,我们发现,原来 mMediaBrowser.connect()主要是实现了捆绑service,这个service从mMediaBrowser的构造函数里可以知道是MusicService.(连接service有两种方式:Started和 Bound.两者的主要区别就是Bound可以实现service和启动他的组件进行交互—通过IBinder.详细的介绍参考官方的开发文档)
## MusicService##
/**
* This class provides a MediaBrowser through a service. It exposes the media library to a browsing
* client, through the onGetRoot and onLoadChildren methods. It also creates a MediaSession and
* exposes it through its MediaSession.Token, which allows the client to create a MediaController
* that connects to and send control commands to the MediaSession remotely. This is useful for
* user interfaces that need to interact with your media session, like Android Auto. You can
* (should) also use the same service from your app's UI, which gives a seamless playback
* experience to the user.
*
* To implement a MediaBrowserService, you need to:
*
* <ul>
*
* <li> Extend {@link android.service.media.MediaBrowserService}, implementing the media browsing
* related methods {@link android.service.media.MediaBrowserService#onGetRoot} and
* {@link android.service.media.MediaBrowserService#onLoadChildren};
* <li> In onCreate, start a new {@link android.media.session.MediaSession} and notify its parent
* with the session's token {@link android.service.media.MediaBrowserService#setSessionToken};
*
* <li> Set a callback on the
* {@link android.media.session.MediaSession#setCallback(android.media.session.MediaSession.Callback)}.
* The callback will receive all the user's actions, like play, pause, etc;
*
* <li> Handle all the actual music playing using any method your app prefers (for example,
* {@link android.media.MediaPlayer})
*
* <li> Update playbackState, "now playing" metadata and queue, using MediaSession proper methods
* {@link android.media.session.MediaSession#setPlaybackState(android.media.session.PlaybackState)}
* {@link android.media.session.MediaSession#setMetadata(android.media.MediaMetadata)} and
* {@link android.media.session.MediaSession#setQueue(java.util.List)})
*
* <li> Declare and export the service in AndroidManifest with an intent receiver for the action
* android.media.browse.MediaBrowserService
*
* </ul>
*
* To make your app compatible with Android Auto, you also need to:
*
* <ul>
慢慢的来看这段介绍:
① 它通过onGetRoot和onLoadChildren 方法把媒体资源提供给客户端。
② 创建MediaSession,并且把MediaSession.Token暴露出来,MediaSession.Token用来创建MediaController(这个前面已经提到过)。
③MusicService继承于MediaBrowserServiceCompat,它需要完成以下要求:
a:实现onGetRoot和onLoadChildren 方法。
b:在onCreate()函数中创建一个MediaSession对象并且通知它的父类(MediaBrowserServiceCompat)。
mSession = new MediaSessionCompat(this, "MusicService");
setSessionToken(mSession.getSessionToken());
c:通过MediaSession#setCallback设置callback对象,这个对象用来接收用户的操作,例如播放,暂停。
mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
到此,一张图总结一下MediaBrowserCompat、MusicService(MediaServiceCompat)和MediaControllerCompat的关系。
MediaControllerCompat 和 MediaSession的详细关系(先有个大题印象):