How to detect input data through the audio jack?

前端 未结 4 1077
耶瑟儿~
耶瑟儿~ 2021-01-31 06:26

I know that some devices headphone ports (maybe all of them? any reference here would be good) have 3 channels, for stereo sound and microphone. So I was wondering if it\'s poss

4条回答
  •  余生分开走
    2021-01-31 06:56

    I finally managed to read the pedal input. @emrys57 is right, replacing the jack with the 4-pin connector, triggered the input the same way microphones with a hook button do. However occasionally it seems to trigger the volume up and the volume down keys as well. I suppose this is related to me replacing the 4-pin jack with a knife and tape.

    It was fairly simple to override onKeyUp. Be aware that you also have to override onKeyDown to prevent the default behavior:

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event){
        int action = event.getAction();
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
                || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                || keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
    
            if (action == KeyEvent.ACTION_UP) {
                Log.d(TAG, "action_up");
                clickStart(null);
                return true;
            } else if (action == KeyEvent.ACTION_DOWN) {
                Log.d(TAG, "action_down");
                return true;
            } else {
                Log.d(TAG, "action:" + action);
                return true;
            }
        }
        return false;
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        int action = event.getAction();
        Log.d(TAG, "onKeyDown!");
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
                || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN
                || keyCode == KeyEvent.KEYCODE_HEADSETHOOK) {
    
            if (action == KeyEvent.ACTION_UP) {
                Log.d(TAG, "action_up");
                clickStart(null);
                return true;
            } else if (action == KeyEvent.ACTION_DOWN) {
                Log.d(TAG, "action_down");
                return false;
            } else {
                Log.d(TAG, "action:" + action);
                return true;
            }
        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {         
            finish();
            return true;
        }
        Log.d(TAG, "returning false");
        return false;
    }
    

提交回复
热议问题