简体   繁体   中英

Remote control client for Android

The RemoteControlClient was introduced in ICS. That's the way the lock screen seems to be integrating with various music players. See the screenshot below for an example of Spotify on the lock screen.

在此输入图像描述

Could one from another app than the lock screen integrate with said players as well?

I find the documentation lacking a bit on the subject, but I think the results, if it's possible, could be interesting.

Edit:

Progress so far: none. What I've found out is that IRemoteControlDisplay likely has some part in it, but my Android/Java skills are a bit lacking to actually understand how to implement it and achieve the same functionality as on the lock screen.

While working on my app I've actually found how to implement your own RemoteControlDisplay.

Basically, you extend IRemoteControlDisplay$Stub, which sends messages to special handler, this handler updates metadata and thing. Then you register your own class extended from IRemoteControlDisplay$Stub by calling to AudioManager#registerRemoteControlDisplay().

And then you unregister it by calling AudioManager#unregisterRemoteControlDisplay().

It's fairly complex, but I've wrote an article on how to this. I've published it on XDA, check it here: http://forum.xda-developers.com/showthread.php?p=44513199

I believe you can do this. However, the method will use private API (the implication is that it may not work on some later version of Android OS).

I recommend to go and download Android source code (http://source.android.com/) and check directory /frameworks/base/media/java/android/media

It has couple of files which are points of your interest: AudioManager.java

AudioService.java

IRemoteControlClient.aidl

IRemoteControlDisplay.aidl

Audio manager has public method, which isn't documented called registerRemoteControlDisplay. You should be able to access it through reflection.

Using this interface you can register an object which implements IRemoteControlDisplay (another undocumented interface) and you should be able to control player through this object.

Try this

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
                AudioManager.AUDIOFOCUS_GAIN);
        Bitmap AlbumArt=BitmapFactory.decodeResource(getResources(), R.drawable.alislahthumbmain);
        mIslahReceiverComponent=new ComponentName(this,AlIslahReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(mIslahReceiverComponent);
        Intent mediaButtonIntent=new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(mIslahReceiverComponent);
        PendingIntent mediaPendingIntent=PendingIntent.getBroadcast(getApplicationContext(),
                0,mediaButtonIntent,0);
        RemoteControlClient mRemoteControlClient=new RemoteControlClient(mediaPendingIntent);
        mRemoteControlClient.editMetadata(true)
        .putString(MediaMetadataRetriever.METADATA_KEY_TITLE,AlIslahApplication.getStreamTitle())
        .putBitmap(100,AlbumArt)
        .apply();
        mRemoteControlClient.setPlaybackState(
                RemoteControlClient.PLAYSTATE_PLAYING);
        mRemoteControlClient.setTransportControlFlags(
               RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE|
                RemoteControlClient.FLAG_KEY_MEDIA_STOP);
        audioManager.registerRemoteControlClient(mRemoteControlClient);

You can't get the same requests to show the display that the lock screen does, but you can certainly trigger the same events that the buttons on this screen do with Broadcast Intents.

The action in question is ACTION_MEDIA_BUTTON and you should attach a KeyEvent with the appropriate keyCode to do what you want.

Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
sendBroadcast(intent);

This will do the same thing as pressing the play/pause button on the lockscreen. You can do this with the other KeyEvent keycodes that make sense (KEYCODE_MEDIA_NEXT, etc...), although you won't know what the currently playing track has registered itself as supporting, while the lockscreen does.

对于任何在KitKat发布版本上遇到此问题的人,您现在可以使用RemoteController ,它连接到RemoteControlClients并允许您控制它们。

You would have to implement the IRemoteControl*.aidl interface in your app and the apps (like Spotify) would have to register itself to your app, which is not the case yet. They register themself to the AudioManager . So NO, you're not able to catch those RemoteClient registrations without either modifying the apps (Spotify, etc..) or modifying the Android system so your app could grab the RemoteClients and their data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM