在 Android 5 上使用 MediaController [英] Using MediaController on Android 5

查看:35
本文介绍了在 Android 5 上使用 MediaController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 5 中使用新的 MediaController 而不是旧的 RemoteController 来获取当前播放的曲目并更改曲目.

I want to use the new MediaController in Android 5 instead of the old RemoteController for getting the currently playing track and changing the track.

我找不到任何示例,所以我自己尝试了.

I couldn't find any examples so I tried it by myself.

为了获得当前的 MediaController,我实现了一个扩展 MediaController.Callback 并实现了 MediaSessionManager.OnActiveSessionsChangedListener 的类.

To get the current MediaController, I have implemented a Class which extends MediaController.Callback and implements MediaSessionManager.OnActiveSessionsChangedListener.

使用这种方法我尝试获取当前的mediaController:

With this method I try to get the current mediaController:

@Override
public void onActiveSessionsChanged(List<MediaController> mediaControllers) {
    Log.i(Util.LOG_TAG, "sessions changed");
    if (mediaControllers != null && !mediaControllers.isEmpty()) {
        this.mediaController = mediaControllers.get(0);
        mediaController.registerCallback(this);
    }
}

但我想我必须注册我的课程.所以我已经完成了

But I think I have to register my class. So I have done

MediaSessionManager mgr =  (MediaSessionManager)context.getSystemService(Context.MEDIA_SESSION_SERVICE);
mgr.addOnActiveSessionsChangedListener(this, null);

但是当我这样做时,我收到一个错误,指出我没有控制媒体的权限.当我尝试添加此权限时,我注意到第三方应用无法使用此权限.

But when I do this I get an Error that I lack the permission to control media. When I tried to add this permission I noticed that this permission can't be used by third party apps.

我做错了什么?

推荐答案

更新:一条评论澄清说,问题实际上是关于查看/控制其他应用的 MediaSession,而不是您自己的.

UPDATE: A comment clarified that the question was actually about viewing/controlling other apps' MediaSessions, not your own.

虽然没有办法直接做到这一点,但出于隐私原因,您有两种选择,它们具有不同级别的可用控件/信息以及对用户交互的不同要求:

Although there is no way to do it directly, due to privacy reasons, you have two options with different levels of available controls/information and different requirements for user interaction:

  1. 如果您只想跳过音乐或播放/暂停,您可以发送媒体按钮事件(通过 AudioManager) 和/或 请求/释放音频焦点.

如果您还需要了解当前正在播放的内容的元数据,有一种更具侵入性的方式需要明确的用户交互:

If you need to also know the metadata of what is currently playing, there is a more intrusive way that requires explicit user interaction:

首先,创建并注册一个NotificationListenerService:

First, create and register a NotificationListenerService:

public class NotificationListener extends NotificationListenerService {
    public NotificationListener() {
    }
}

在 AndroidManifest.xml 中:

In AndroidManifest.xml:

<service android:name=".NotificationListener"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    android:enabled="true" android:exported="true">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

然后您将能够通过在 getActiveSessions 调用中指定您的 NotificationListenerService componentName 来获取 MediaSessions:

Then you will be able to fetch MediaSessions by specifying your NotificationListenerService componentName in the getActiveSessions call:

MediaSessionManager mm = (MediaSessionManager) this.getSystemService(
    Context.MEDIA_SESSION_SERVICE);
List<MediaController> controllers = mm.getActiveSessions(
    new ComponentName(this, NotificationListener.class));
Log.i(TAG, "found " + controllers.size() + " controllers");

一个警告是,用户需要通过转到设置"->声音和"来明确授予您的应用通知访问权限.通知 -> 通知访问

One caveat is that the user will need to explicitly give your app Notification access permission, by going to Settings -> Sound & Notification -> Notification access

描述您的 MediaSession 并且可以传递以允许其他组件/应用程序控制现有 MediaSession 的对象是 MediaSession.Token.使用令牌,您可以直接创建 MediaController,而无需求助于 MediaSessionManager.代码类似于:

The object that describes your MediaSession and that can be passed along to allow other components/apps to control an existing MediaSesion is the MediaSession.Token. With a token, you can create a MediaController directly, without resorting to the MediaSessionManager. The code for that would be something like:

MediaController mediaController = new MediaController(getActivity(),
    sessionToken);

这不需要任何特殊许可.如果您还使用 MediaBrowser 或 MediaBrowserService,您应该使用它的 getSessionToken() 方法获取与 MediaBrowser 关联的令牌.

This doesn't require any special permission. If you are also using the MediaBrowser or the MediaBrowserService, you should get the token associated with the MediaBrowser, by using it's getSessionToken() method.

我们刚刚发布了一个示例,它使用 MediaBrowserService 来处理音乐浏览、播放和媒体风格通知并提供一个简单的 Activity 来控制播放.

We just released an example that uses a MediaBrowserService to handle music browsing, playback and media style notification and provides a simple Activity to control playback.

这篇关于在 Android 5 上使用 MediaController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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