java 安卓远程控制客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12168046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:52:52  来源:igfitidea点击:

Remote control client for Android

javaandroid

提问by Frans

The RemoteControlClientwas 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.

RemoteControlClient在ICS推出。这就是锁屏似乎与各种音乐播放器集成的方式。有关锁定屏幕上的 Spotify 示例,请参阅下面的屏幕截图。

enter image description here

在此处输入图片说明

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 IRemoteControlDisplaylikely 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.

迄今为止的进展:无。我发现IRemoteControlDisplay可能有一些部分,但我的 Android/Java 技能有点缺乏真正理解如何实现它并实现与锁定屏幕相同的功能。

采纳答案by Alexander Woodblock

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

在开发我的应用程序时,我实际上已经找到了如何实现您自己的 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().

基本上,您扩展 IRemoteControlDisplay$Stub,它将消息发送到特殊处理程序,此处理程序更新元数据和事物。然后通过调用 AudioManager#registerRemoteControlDisplay() 注册从 IRemoteControlDisplay$Stub 扩展的自己的类。

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

然后通过调用 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

这相当复杂,但我已经写了一篇关于如何做到这一点的文章。我已经在 XDA 上发布了它,请在此处查看:http: //forum.xda-developers.com/showthread.php?p=44513199

回答by Victor Ronin

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).

我相信你可以做到这一点。但是,该方法将使用私有 API(这意味着它可能不适用于某些更高版本的 Android 操作系统)。

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

我建议去下载Android源代码(http://source.android.com/)并检查目录/frameworks/base/media/java/android/media

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

它有几个您感兴趣的文件:AudioManager.java

AudioService.java

音频服务.java

IRemoteControlClient.aidl

远程控制客户端.aidl

IRemoteControlDisplay.aidl

远程控制显示.aidl

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

音频管理器具有公共方法,该方法未记录在案,称为 registerRemoteControlDisplay。您应该能够通过反射访问它。

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.

使用这个接口,你可以注册一个实现 IRemoteControlDisplay(另一个未公开的接口)的对象,你应该能够通过这个对象控制播放器。

回答by itsmewajid

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);

回答by chrisrhoden

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.

有问题的操作是 ACTION_MEDIA_BUTTON,您应该使用适当的 keyCode 附加 KeyEvent 以执行您想要的操作。

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.

这与按下锁屏上的播放/暂停按钮的作用相同。您可以使用其他有意义的 KeyEvent 键码(KEYCODE_MEDIA_NEXT 等)来执行此操作,尽管您不知道当前播放的曲目已将自己注册为支持什么,而锁定屏幕却可以。

回答by ter0

For anyone stumbling on this question post KitKat release, you can now use the RemoteController, which connects to RemoteControlClients and allows you to control them.

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

回答by chrulri

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.

您必须在您的应用程序中实现 IRemoteControl*.aidl 接口,并且应用程序(如 Spotify)必须将自己注册到您的应用程序,但目前情况并非如此。他们将自己注册到AudioManager。所以不,您无法在不修改应用程序(Spotify 等)或修改 Android 系统的情况下捕获这些 RemoteClient 注册,以便您的应用程序可以获取 RemoteClients 及其数据。