Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android respond bluetooth audio buttons in car - ExoPlayer

I have made android audio application. And now some users reporting that app is not responding on media buttons (play, pause, next) when they connected the device via Bluetooth in the car.

At the same time, I tested app with Bluetooth speaker - play/pause button works as needed.

Are there any differences between Bluetooth speaker and Bluetooth radio in the car??? It is very strange

I'm using ExoPlayer and have this code for handling media buttons:

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, getApplicationContext(), MediaButtonReceiver.class);
    mSession.setMediaButtonReceiver(PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0));
    MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mSession, new DefaultPlaybackController(), false, null);
    mediaSessionConnector.setQueueNavigator(new QueueNavigatorAdapter() {
        @Override
        public void onSkipToPrevious(Player player) {
            playPrevious();
        }

        @Override
        public void onSkipToNext(Player player) {
            playNext();
        }
    });
    mediaSessionConnector.setPlayer(mPlayer, null);

and

    mPlayer.prepare(source);
    mSession.setActive(true);
    mPlayer.setPlayWhenReady(true);

I know about AudioManager.registerMediaButtonEventReceiver() and handling KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE etc. but I was sure mSession.setMediaButtonReceiver && MediaSessionConnector do it for me.

Maybe I'm wrong and it is not enough?

minSdkVersion 21

like image 520
Dmitry Avatar asked Aug 17 '18 09:08

Dmitry


1 Answers

As I remember, you need to perform something like this:

if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode()==25)

based on Bluetooth device you are working with, in this case, Bluetooth radio in the car. You need manual for car radio device.

like image 133
Stanojkovic Avatar answered Sep 29 '22 03:09

Stanojkovic