1

I'm working on an android music player and want to skip tracks using long press volume keys. I've found some implementations, but none of them worked as I want it. I want to skip song on long press and change volume on short press (normal behavior). In all tutorials/answers they handled long press with onKeyDown and onKeyLongPress but the short/normal press was only logged in onKeyUp.

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    Log.d(TAG, "long pressed");
    switch(keyCode){
    case KeyEvent.KEYCODE_VOLUME_UP:
        songNumber++;
        playSong(songs.get(songNumber));
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        songNumber--;
        playSong(songs.get(songNumber));
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode){
    case KeyEvent.KEYCODE_VOLUME_UP:
        event.startTracking();
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch(keyCode){
    case KeyEvent.KEYCODE_VOLUME_UP:
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

This is my code but with this I can only skip songs and short press has no effect. So I have only one volume level :(

I have no idea what to try now. Help is appreciated. Thanks!

-v1r0x

v1r0x
  • 31
  • 7
  • You should post your solution as an answer and accept it. – Sam Nov 07 '12 at 00:50
  • my solution is the block below "EDIT". How can I accept it? I'm new to stackoverflow – v1r0x Nov 08 '12 at 02:21
  • Simply cut & paste your solution from your question to an answer, there will be a large green check mark to click which accepts it. This way anyone searching for answers to a similar problem can see that this has been answered from outside the question. Also anyone who finds your answer useful can upvote it and give you some reputation points. :) – Sam Nov 08 '12 at 17:02
  • http://stackoverflow.com/questions/19883383/android-onkeylongpress-when-webview-exists – trante Jan 16 '14 at 22:44

1 Answers1

1

Ok, I have found a solution. Not the best, but it works. OnKeyDown and OnKeyLongPress are still the same, but OnKeyUp is now this:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if(!event.isCanceled()){
        switch(keyCode){
        case KeyEvent.KEYCODE_VOLUME_UP:
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
            break;
        }
    }
    return super.onKeyUp(keyCode, event);
}
v1r0x
  • 31
  • 7