0

我正在实现媒体按钮服务来处理耳机(蓝牙或有线)的媒体按钮事件,对于某些特定耳机(例如:BOSE QC35、APPLE airpods),该服务无法检测到第一次点击的事件按钮,但可以检测它的下一次点击。

例如:

假设我想在单击 BOSE QC35/AirPods 的媒体按钮后结束互联网通话,单击媒体按钮后通话不会结束,而是将通话路由到手机(我也可以在 QC35 上听到“通话结束”的声音点),当我再次单击媒体按钮时,通话将结束。

对于同样的实现,如果我使用 Monster powerbeats2 或任何有线耳机,通话将在第一次点击后结束

这是代码:

public class SampleMediaButtonService extends MediaBrowserServiceCompat {

    private MediaSessionCompat mSession;
    private static final int POSITION = 0;
    private static final float PLAYBACK_SPEED = 1f;

    @Override
    public void onCreate() {
        super.onCreate();
        initMediaSessions();
    }

    @Nullable
    @Override
    public BrowserRoot onGetRoot(@NonNull final String clientPackageName, final int clientUid,
                                 @Nullable final Bundle rootHints) {

        return null;
    }

    @Override
    public void onLoadChildren(@NonNull final String parentId,
                               @NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {


    }

    private void initMediaSessions() {
        mSession = new MediaSessionCompat(getApplicationContext(),
                MediaButtonService.class.getSimpleName());
        mSession.setFlags(
                MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
        setSessionToken(mSession.getSessionToken());

        mSession.setCallback(
                new MediaSessionCompat.Callback() {
                    @Override
                    public void onPlay() {
                        LOG.i("Handling media button click");

                        if (//CALL IS ACTIVE) {
                            //END the call
                        } 
                    }
                }
        );

        /* Keeping the initial state as STATE_PAUSED. This will ensure when button is clicked
         * onPlay() is called.
         */
        long actions = PlaybackStateCompat.ACTION_PLAY;
        PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
                .setActions(actions)
                .setState(PlaybackStateCompat.STATE_PAUSED, POSITION, PLAYBACK_SPEED)
                .build();
        mSession.setPlaybackState(playbackStateCompat);
        mSession.setActive(true);
    }

    @Override
    public int onStartCommand(final Intent startIntent, final int flags, final int startId) {
        if (startIntent != null) {
            // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
            MediaButtonReceiver.handleIntent(mSession, startIntent);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSession.release();
    }
}
4

0 回答 0