android - 从客户端本身停止时,锁屏远程控制客户端上的奇怪闪烁

标签 android remote-control

我目前正在构建一个流式 Android 应用程序,并且我正在尝试集成一个远程控制客户端(例如,从 ICS+ 的锁定屏幕进行控制)。

为此,我在启动流媒体服务时这样做:

    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        stopSelf();
    }

    mediaButtonReceiverComponent = new ComponentName(this, RemoteControlReceiver.class);
    audioManager.registerMediaButtonEventReceiver(mediaButtonReceiverComponent);

    if (remoteControlClientCompat == null) {
        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
        remoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(
                        getApplicationContext(),
                        0,
                        mediaButtonIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                )
        );
        RemoteControlHelper.registerRemoteControlClient(audioManager, remoteControlClientCompat);
    }

    final int flags = RemoteControlClient.FLAG_KEY_MEDIA_STOP;
    remoteControlClientCompat.setTransportControlFlags(flags);

remoteControlClientCompat 只是 RemoteControlClientCompat 的一个实例来自样本。

然后在流式传输期间我正在更新元数据。一切正常,甚至控件也发送到我的 RemoteControlReceiver。数据和图像在锁定屏幕上显示得很好。

从我的应用程序停止流式传输会破坏锁定屏幕的东西,但是当我试图从小部件本身(通过按下停止按钮)破坏它时,它会做一些奇怪的事情。按下停止按钮会使广播接收器停止我的流媒体服务。然后在服务的 onDestroy() 方法中,我这样做:

    RemoteControlHelper.unregisterRemoteControlClient(audioManager, remoteControlClientCompat);
    audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiverComponent);
    audioManager.abandonAudioFocus(this);

一旦调用 audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiverComponent);,小部件就会闪烁。我试过评论该行,闪烁发生在 audioManager.abandonAudioFocus(this); 中。当服务停止时,注释其他行也会使其闪烁。

我注意到当我从通知中停止流式传输时也会发生这种情况。

我做错了什么?我尝试更改此调用的顺序但无法解决。 我注意到 Spotify 在几个版本前有完全相同的问题。我想知道他们是如何解决的...

最佳答案

好的,我修好了。这只是因为当我们放弃音频焦点时 RemoteControlClient 无法播放。所以我只需要在销毁任何东西之前调用它:

remoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);

关于android - 从客户端本身停止时,锁屏远程控制客户端上的奇怪闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17717359/

相关文章:

grails - Grails远程控制插件-Spring Security Configuration

c++ - 使用 c++ 和 ubuntu 与 Futaba RC Controller 接口(interface)

github - Github 或 GItLab 网站上是否提供用于远程存储库的终端?

android - 是否可以使用数码相机读取红外 Remote 的代码?

android - 在使用自定义字体或字样的 EditText 中绘制背景线

android - 如何使用包管理器获取 Android 设备功能

android - 编辑 Activity 的上下文操作栏

ios - remoteControlReceivedWithEvent 不在单例类中调用

android - 自动设置 Android CTS 测试

java - 在Android中制作YesActionBar和NoActionBar主题并在应用程序中使用它们