如何从MediaSessionManager创建MediaController实例? [英] How can I create a MediaController instance from MediaSessionManager?

查看:132
本文介绍了如何从MediaSessionManager创建MediaController实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Lollipop的RemoteController迁移到MediaController,但是我在获取MediaController实例时遇到了麻烦.在MediaController中说:

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

如果您拥有"android.permission.MEDIA_CONTENT_CONTROL"权限,或者是启用的通知侦听器,或者直接从会话所有者获取MediaSession.Token,则可以通过MediaSessionManager创建MediaController.

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.

但是,当我查看MediaSessionManager时,除了addOnActiveSessionsChangedListener,getActiveSessions和removeOnActiveSessionsChangedListener之外,没有其他方法对我没有帮助. 我尝试从MediaSession创建令牌,但是这无法使我的回调从其他媒体播放器获取任何信息.

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.

推荐答案

MediaController.registerCallback()来注册 MediaController.Callback 创建的实现,该实现与已弃用的 RemoteController.OnClientUpdateListener (或仅将媒体控件直接发送回MediaController.

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.

当然,在大多数情况下,您希望将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.

这篇关于如何从MediaSessionManager创建MediaController实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆