Using MediaController on Android 5

I want to use the new one MediaController

in Android 5 instead of the old one RemoteController

to get the currently playing track and change the track.

I couldn't find examples, so I tried it myself.

To get the current one MediaController

, I implemented a class that extends MediaController.Callback

and implements MediaSessionManager.OnActiveSessionsChangedListener

.

With this method I am trying to get the current one 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 need to register my class. So I did

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

      

But when I do that, I get an error that I am missing permission to manage media. When I tried to add this permission, I noticed that this permission cannot be used by third party apps.

What am I doing wrong?

+3


source to share


2 answers


UPDATE : The comment clarified that it was actually about viewing / controlling the MediaSessions of other apps and not your own.

While this cannot be done directly, due to privacy considerations, you have two options with different levels of available controls / information and different requirements for user interaction:

  • If you just want to skip music or play / pause, you can send media button events (via AudioManager ) and / or audio focus request / release .

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

First, create and register a NotificationListenerService:

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

      

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>

      

Then you can get the MediaSessions by specifying the component name NotificationListenerService 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 needs to explicitly grant your permission to access your app's notification by going to Settings -> Sound & Notification -> Access Notifications

Original answer:

The object that describes your MediaSession and that can be passed so that other components / applications can manage the existing MediaSesion is MediaSession.Token. With the token, you can create a MediaController directly without resorting to the MediaSessionManager. The code for this will be something like this:

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

      

This does not require special permission. If you are also using MediaBrowser or MediaBrowserService, you must get the token associated with the MediaBrowser using the getSessionToken () method.

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

+11


source


Check for the next permission



private void notificationAccessPermission() {
    if (Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName())) {
        //service is enabled do something
    } else {
        Intent intent = new
                Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        startActivity(intent);
    }
}

      

0


source







All Articles