Android Question Head Phone Jack push detection

ocalle

Active Member
Licensed User
Longtime User
Hello, I continue finding a solution to headphone jack click detection, attached is a code of some dude in stackoverflow that found a solution that detect when the jack button is pressed and the delay that remain on it.

audioSession = new MediaSession(getApplicationContext(), "TAG");
audioSession.setCallback(new MediaSession.Callback() {

@override
public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
String intentAction = mediaButtonIntent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

if (event != null) {

stopTimeOfGame_millis = event.getDownTime();
double usersReactionTime = (event.getDownTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getDownTime(): " + usersReactionTime);


double getEventTime = (event.getEventTime() - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("event.getEventTime(): " + getEventTime);

int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
long action_down = android.os.SystemClock.uptimeMillis();
double actionDown = (action_down - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_DOWN: " + actionDown);
}

if (action == KeyEvent.ACTION_UP) {
long action_up = android.os.SystemClock.uptimeMillis();
double actionUp = (action_up - startTimeOfGame_millis) / 1000.0;
UtilsRG.info("ACTION_UP: " + actionUp);
}
}
}
return true;
}


});

PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE)
.setState(PlaybackState.STATE_PLAYING, 0, 0, 0)
.build();
audioSession.setPlaybackState(state);

audioSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

audioSession.setActive(true);

The results on log was

got following log:

event.getDownTime(): 0.281

event.getEventTime(): 0.421

ACTION_DOWN: 0.47

ACTION_UP: 0.471

Thus now I got the moment when the user presses the key down.



I dont know how translate this to B4a to check if it work.

Regards

Osvaldo
"Share is grow, no matter how many you share there are too many people in the world to do business, share and grow"
 
Top