How to detect and override media button key events on bluetooth headset?

Okay so I know many similar questions have been asked but nothing is working so far. I have tried all the mediaSessionCompat methods and callbacks but to no luck. I have gone through the documentation but it is pretty vague especially regarding the new practices. I basically want to override the media buttons of my bluetooth headset and stop the default music player from playing.

EDIT: So after going deeper in to the documentation I came up with the following:

MediaPlaybackService.java

public class MediaPlaybackService extends Service {     @Nullable     @Override     public IBinder onBind(Intent intent) {         return null;     }     private MediaSessionCompat.Callback mediaSessionCompatCallBack = new MediaSessionCompat.Callback()     {         @Override         public void onPlay() {             super.onPlay();             Toast.makeText(getApplication(),"Play Button is pressed!",Toast.LENGTH_SHORT).show();         }          @Override         public void onPause() {             super.onPause();             Toast.makeText(getApplication(),"Pause Button is pressed!",Toast.LENGTH_SHORT).show();         }          @Override         public void onSkipToNext() {             super.onSkipToNext();             Toast.makeText(getApplication(),"Next Button is pressed!",Toast.LENGTH_SHORT).show();         }          @Override         public void onSkipToPrevious() {             super.onSkipToPrevious();             Toast.makeText(getApplication(),"Previous Button is pressed!",Toast.LENGTH_SHORT).show();         }          @Override         public void onStop() {             super.onStop();             Toast.makeText(getApplication(),"Stop Button is pressed!",Toast.LENGTH_SHORT).show();         }          @Override         public boolean onMediaButtonEvent(Intent mediaButtonEvent) {             String intentAction = mediaButtonEvent.getAction();              if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))             {                 KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);                  if (event != null)                 {                     int action = event.getAction();                     if (action == KeyEvent.ACTION_DOWN) {                         switch (event.getKeyCode()) {                             case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:                                 // code for fast forward                                 return true;                             case KeyEvent.KEYCODE_MEDIA_NEXT:                                 // code for next                                 return true;                             case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:                                 // code for play/pause                                 Toast.makeText(getApplication(),"Play Button is pressed!",Toast.LENGTH_SHORT).show();                                 return true;                             case KeyEvent.KEYCODE_MEDIA_PREVIOUS:                                 // code for previous                                 return true;                             case KeyEvent.KEYCODE_MEDIA_REWIND:                                 // code for rewind                                 return true;                             case KeyEvent.KEYCODE_MEDIA_STOP:                                 // code for stop                                 Toast.makeText(getApplication(),"Stop Button is pressed!",Toast.LENGTH_SHORT).show();                                 return true;                          }                         return false;                      }                     if (action == KeyEvent.ACTION_UP) {                      }                 }             }             return super.onMediaButtonEvent(mediaButtonEvent);         }     };      private MediaSessionCompat mediaSessionCompat;       @Override     public void onCreate() {         Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();         Log.e("SERVICE", "onCreate");          mediaSessionCompat = new MediaSessionCompat(this, "MEDIA");         mediaSessionCompat.setCallback(mediaSessionCompatCallBack);         mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);         PlaybackStateCompat.Builder mStateBuilder = new PlaybackStateCompat.Builder()                 .setActions(                         PlaybackStateCompat.ACTION_PLAY |                                 PlaybackStateCompat.ACTION_PAUSE |                                 PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |                                 PlaybackStateCompat.ACTION_SKIP_TO_NEXT |                                 PlaybackStateCompat.ACTION_PLAY_PAUSE);          mediaSessionCompat.setPlaybackState(mStateBuilder.build());         mediaSessionCompat.setActive(true);      }      @Override     public void onDestroy() {         Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();         Log.e("SERVICE", "onDestroy");         mediaSessionCompat.release();     }      @Override     public int onStartCommand(Intent intent, int flags, int startId) {         Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();         Log.e("SERVICE_STARTUP", "onStart");          MediaButtonReceiver.handleIntent(mediaSessionCompat, intent);          return super.onStartCommand(intent, flags, startId);     } }  

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="pk.mohammadadnan.tunalapp">      <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />     <uses-feature android:name="android.hardware.bluetooth" />     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />     <uses-permission android:name="android.permission.BLUETOOTH" />     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />      <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>          </activity>         <receiver android:name="androidx.media.session.MediaButtonReceiver" >             <intent-filter>                 <action android:name="android.intent.action.MEDIA_BUTTON" />             </intent-filter>         </receiver>         <service android:name="pk.mohammadadnan.tunalapp.MediaPlaybackService" >             <intent-filter>                 <action android:name="android.intent.action.MEDIA_BUTTON" />             </intent-filter>         </service>     </application>  </manifest> 

MainActivity.java

public class MainActivity extends AppCompatActivity {       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          startService(new Intent(this, MediaPlaybackService.class));               } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.