How can I create a MediaController instance from MediaSessionManager?

ⅰ亾dé卋堺 提交于 2019-12-30 11:34:12

问题


I am trying to move from RemoteController to MediaController for Lollipop but I'm having trouble getting a MediaController instance. In MediaController it says:

A MediaController can be created through MediaSessionManager if you hold the "android.permission.MEDIA_CONTENT_CONTROL" permission or are an enabled notification listener or by getting a MediaSession.Token directly from the session owner.

However when I look at MediaSessionManager there are no methods other than addOnActiveSessionsChangedListener, getActiveSessions, and removeOnActiveSessionsChangedListener these help me none. I tried creating a token from a MediaSession but that didn't enable my callback to get anything from other media players.


回答1:


MediaSessionManager.getActiveSessions() gives you a list of MediaController instances associated with the currently playing music player(s). You can then use MediaController.registerCallback() to register a MediaController.Callback implementation you've created, which has similar callbacks to the deprecated RemoteController.OnClientUpdateListener (or just send media controls directly back to the MediaController.

Of course, in most cases you want to keep your list of MediaControllers in sync with the system: that is what MediaSessionManager.addOnActiveSessionsChangedListener() is for: it gives you an updated list of MediaControllers with which to use and is called every time the list of players change. As per the documentation, if you are using the permission you can pass null in for the ComponentName notificationListener parameter.



来源:https://stackoverflow.com/questions/26569044/how-can-i-create-a-mediacontroller-instance-from-mediasessionmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!