Skip to content Skip to sidebar Skip to footer

How Register Broadcast Receiver For Media Button In Oreo?

I have a problem with the new Android version, that is 8.0 (Oreo). I have to register a broadcast and I do this with this code: // android.intent.action.MEDIA_BUTTON IntentFilter f

Solution 1:

If you want to receive media button, you have to play audio and use mediasession.

i.g

MediaSessionms=newMediaSession(getApplicationContext(), getPackageName());
    ms.setActive(true);

    ms.setCallback(newMediaSession.Callback() {
        @OverridepublicbooleanonMediaButtonEvent(Intent mediaButtonIntent) {
            Log.e("hmhm", "hmhm media button");
            returnsuper.onMediaButtonEvent(mediaButtonIntent);
        }
    });

    // you can button by receiver after terminating your app
    ms.setMediaButtonReceiver(mbr); 

    // play dummy audioAudioTrackat=newAudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
    at.play();

    // a little sleep 
    at.stop();
    at.release();

Post a Comment for "How Register Broadcast Receiver For Media Button In Oreo?"