Home > Mobile >  Android headset wire control problem, want to use MediaSession intercept the headset button click ev
Android headset wire control problem, want to use MediaSession intercept the headset button click ev

Time:09-24

Specific how to operate???

Online is a browser session + + browserService + Controller together, I don't have to so complicated, I just want to simple click events send a radio interception headphones, open an activity of what, what don't have to play the music

Guide for bosses, very few points, a few days in accordance with the drawings to see


Android

CodePudding user response:

solved

MSession=new MediaSessionCompat (this, "MusicService");
MSession. SetCallback (new MediaSessionCompat. Callback () {
@ Override
Public Boolean onMediaButtonEvent (Intent mediaButtonEvent) {
String intenAction=mediaButtonEvent. GetAction ();

If (intenAction. Equals (Intent. ACTION_MEDIA_BUTTON)) {

KeyEvent event=mediaButtonEvent. GetParcelableExtra (Intent. EXTRA_KEY_EVENT);
If (event!=null) {
Int action=event. GetAction ();
If (action==KeyEvent. ACTION_DOWN) {

Int keyCode=event. GetKeyCode ();
LogUtil. D (" -- -- MediaSession event. GetKeyCode ()="+ event. GetKeyCode ());

The switch (keyCode) {
Case KeyEvent. KEYCODE_HEADSETHOOK:

LogUtil. D (" MediaSession ordinary headphones play/pause keyCode="+ KeyEvent. KEYCODE_HEADSETHOOK);

break;

Case KeyEvent. KEYCODE_MEDIA_PLAY:

LogUtil. D (" bluetooth headset - MediaSession play keyCode="+ KeyEvent. KEYCODE_MEDIA_PLAY);

break;

Case KeyEvent. KEYCODE_MEDIA_PAUSE:

LogUtil. D (" -- -- MediaSession bluetooth headset to suspend keyCode="+ KeyEvent. KEYCODE_MEDIA_PAUSE);

break;

Case KeyEvent. KEYCODE_MEDIA_NEXT:

LogUtil. D (" -- -- MediaSession bluetooth headset next song keyCode="+ KeyEvent. KEYCODE_MEDIA_NEXT);


break;

Case KeyEvent. KEYCODE_MEDIA_PREVIOUS:

LogUtil. D (" -- -- MediaSession bluetooth headset on a curved keyCode="+ KeyEvent. KEYCODE_MEDIA_PREVIOUS);

break;
}

}
}

}
Return super. OnMediaButtonEvent (mediaButtonEvent);
}

});

MSession. SetFlags (MediaSessionCompat FLAG_HANDLES_MEDIA_BUTTONS |/media/FLAG_HANDLES_MEDIA_BUTTONS control button
MediaSessionCompat. FLAG_HANDLES_TRANSPORT_CONTROLS);//FLAG_HANDLES_TRANSPORT_CONTROLS control transmission command

MSession. SetActive (true);//activate
//mSession. Release ();//exits to destroy
//mSession setPlaybackState (state);////update the status and the most important information
//mSession. SetMetadata (MediaMetadataCompat metadata);//media_session services, can be through the command to view system information on current service adb shell dumpsys media_session
  • Related