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

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

问题描述

我想使用新的MediaController Android中5,而不是老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 would 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.

我是什么做错了吗?
感谢您的anwers!

What am I doing wrong? Thanks for your anwers!

推荐答案

更新
注释澄清,这个问题实际上是有关查看/控制其他应用的MediaSessions,而不是你自己的。

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. 如果您只是想跳过的音乐或播放/暂停,您可以发送的媒体按钮事件(通过<一个href=\"http://developer.android.com/reference/android/media/AudioManager.html#dispatchMediaKeyEvent(android.view.KeyEvent)\"相对=nofollow> AudioManager )和/或<一个href=\"http://stackoverflow.com/questions/11275400/reliably-pausing-media-playback-system-wide-in-android\">request/release音频重点。

如果您还需要了解什么是当前正在播放的元数据,也就是需要显式用户交互的更侵入性的方式:

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组件名获取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和可以一起传递,以允许其它组件/应用程式,以控制现有MediaSesion是MediaSession.Token的对象。有了一个道理,你可以直接创建的MediaController,而不诉诸MediaSessionManager。在code表示这将是这样的:

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来处理音乐浏览,回放和媒体风格通知并提供一个简单的活动来控制播放。

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上使用的MediaController 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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