安卓“O"(Oreo, 8) 和更高版本的媒体按钮问题 [英] Android "O" (Oreo, 8) and higher media buttons issue

查看:17
本文介绍了安卓“O"(Oreo, 8) 和更高版本的媒体按钮问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Text-to-Speech 应用程序中使用的处理耳机媒体按钮的代码在 Android API 22 到 25 下运行良好(在旧版本的 Android 中,它们由其他现已贬值的方式处理).但是,在 Android 8 Oreo"(公开测试版和最终版)下,它不起作用.相关代码如下:

The code for handling media buttons from headsets that I use in my Text-to-Speech app works great under Android API 22 through 25 (in older versions of Android they are handled by other, now depreciated means). However under Android 8 "Oreo", both public beta and final release, it does not work. Here is the relevant code:

服务启动时,我创建 MediaSessionCompact 对象:

When the service starts, I create MediaSessionCompact object:

        mSession = new MediaSessionCompat(getApplicationContext(), "my.package.name._player_session");
        mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mSession.setActive(true);
        mSession.setCallback(myMediaSessionCallback);
        PlaybackStateCompat state = new PlaybackStateCompat.Builder()
                .setActions(ACTION_PLAY_PAUSE | ACTION_PLAY | ACTION_PAUSE |
                        ACTION_SKIP_TO_NEXT | ACTION_SKIP_TO_PREVIOUS |
                        ACTION_FAST_FORWARD | ACTION_REWIND
                )
                .setState(PlaybackStateCompat.STATE_PAUSED, 0 /*PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN*/, 1f)
                .build();
        mSession.setPlaybackState(state);

当然定义了会话媒体回调:

There is of course session media callback defined:

private MediaSessionCompat.Callback myMediaSessionCallback = new MediaSessionCompat.Callback() {
    @Override
    public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
        // The log output below never appears on "Oreo", nothing comes here.
        Log.d(TAG, "callback onMediaButtonEvent() Compat");
        MediaButtonIntentReceiver.handleIntent(mediaButtonIntent.getAction(), (KeyEvent) mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT));
        return true;
    }

    @Override
    public void onSkipToNext() {
        //...
    }
    // etc. other overrides
};

我还尝试了 PendingIntent,使用 MediaButtonReceiver.buildMediaButtonPendingIntent() 并为我感兴趣的所有操作设置 mSession.setMediaButtonReceiver(pendingIntent),然后在我的服务 onStartCommand() 中调用 MediaButtonReceiver.handleIntent(mSession, intent):

I also experimented with PendingIntent, using MediaButtonReceiver.buildMediaButtonPendingIntent() and set mSession.setMediaButtonReceiver(pendingIntent) for all the actions I'm interested in, then in my service onStartCommand() I call MediaButtonReceiver.handleIntent(mSession, intent):

// still in the same service:            
mSession.setMediaButtonReceiver(
            MediaButtonReceiver.buildMediaButtonPendingIntent(
                this,
                mMediaButtonReceiverComponentName,
                ACTION_PLAY));


mSession.setMediaButtonReceiver(
            MediaButtonReceiver.buildMediaButtonPendingIntent(
                this,
                mMediaButtonReceiverComponentName,
                ACTION_PAUSE));


mSession.setMediaButtonReceiver(
            MediaButtonReceiver.buildMediaButtonPendingIntent(
                this,
                mMediaButtonReceiverComponentName,
                ACTION_PLAY_PAUSE));

在服务 onStartCommand() 中:

and in the service onStartCommand():

@Override 
public int onStartCommand(Intent intent, int flags, int startId) {
    // ...
    if (intent != null) {
        MediaButtonReceiver.handleIntent(mSession, intent);
        // ...
    }
    return START_NOT_STICKY;
}

没什么,媒体按钮按下事件完全是愚蠢的.O"或我的代码有什么问题???我完全不知所措.

Nothing, it's completely dumb to media buttons press events. What's wrong with "O" or my code there??? I'm completely baffled.

2017 年 8 月 32 日更新

我还创建了一个简单但有效的应用程序项目来演示该问题,请参阅:https://github.com/gregko/PlayerServiceSample.该项目在 Android 5.x 到 7.x 下的耳机上按下媒体按钮时显示 LogCat 输出,但在 Android 8 "Oreo" 下完全失败.

I also created a trivial but working app project that demonstrates the problem, please see: https://github.com/gregko/PlayerServiceSample. This project displays LogCat output when a media button is pressed on a headset under Android 5.x to 7.x, but fails completely under Android 8 "Oreo".

2017 年 9 月 1 日更新Android 问题跟踪器上现在有一个关于此的未解决问题,我已提交,位于 https://issuetracker.google.com/issues/65175978.媒体按钮仍然可以在我在 Oreo 上测试的几个音乐播放器应用程序中工作,我只是不知道它们做了什么不同的工作来使它们工作......我的应用程序的上下文不是播放音乐,而是用文本朗读文本到语音服务,因此音乐播放器示例中的许多代码不适用.

Update 9/1/2017 There is now an open issue on Android Issue Tracker about this, which I submitted, at https://issuetracker.google.com/issues/65175978. Still the media buttons work in several music player apps I tested on Oreo, I just can't figure out what do they do differently to make them work... The context of my app is not playing music, but reading aloud text with Text to Speech service, so a lot of code from Music Player examples does not apply.

推荐答案

已解决.关于Android 8.0 行为变化"我们在 Google 页面上找到了这样的文字:

Solved. On "Android 8.0 Behavior Changes" Google page we find this text:

在 Android 8.0(API 级别 26)中,媒体按钮事件的处理有所不同:

In Android 8.0 (API level 26) the handling of media button events is different:

  1. UI Activity 中媒体按钮的处理没有改变:前台 Activity 在处理媒体按钮事件时仍具有优先权.
  2. 如果前台活动不处理媒体按钮事件,系统会将事件路由到最近在本地播放音频的应用程序.在确定哪个应用接收媒体按钮事件时,不会考虑媒体会话的活动状态、标志和播放状态.
  3. 如果应用的媒体会话已被释放,系统会将媒体按钮事件发送到应用的 MediaButtonReceiver(如果有).
  4. 对于其他所有情况,系统都会丢弃媒体按钮事件.

为了让我的微不足道的样本工作,我所要做的就是使用 MediaPlayer 播放一些声音.显然,使用 Text-to-Speech API 播放声音不符合条件,我认为这是一个错误.

All I had to do to make my trivial sample work was to play some sound with MediaPlayer. Apparently playing sound with Text-to-Speech API does not qualify, which in my opinion is a bug.

这是我添加到我的简单示例中以使其工作的代码,从原始资源目录播放一个非常简短且无声的 WAV 文件:

Here is the code I added to my trivial sample to make it work, playing a very brief and silent WAV file from Raw resources directory:

    final MediaPlayer mMediaPlayer;
    mMediaPlayer = MediaPlayer.create(this, R.raw.silent_sound);
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            mMediaPlayer.release();
        }
    });
    mMediaPlayer.start();

更新

https://issuetracker.google.com/issues/65344811 上向 Android 问题跟踪器提交了错误报告

更新 2,2017 年 10 月 10 日

Google 现在表示 Oreo 在这方面的行为是设计使然";并且不会修复它.阅读上面问题跟踪器帖子末尾附近的回复.我必须说我很失望.

Google now says that Oreo behavior in this respect is "by design" and won't fix it. Read the reply near the end of the issue tracker post above. I must say I'm disappointed.

这篇关于安卓“O"(Oreo, 8) 和更高版本的媒体按钮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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