How to detect input data through the audio jack?

前端 未结 4 1075
耶瑟儿~
耶瑟儿~ 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:52

    There are two potential paths of possibility here.

    • One would be to use the headset (or perhaps more specifically microphone) presence detection circuitry, and read that status via the appropriate API. This would be fairly simple (just have to figure out what property of a real headset is being measured). However, there is a good chance that the response may be fairly slow, as the phone is likely to go through some audio configuration changes and will not want to do that until it is "sure" that the headset has been connected or disconnected. Also the simplest non-microphone headset detection may be via a mechanical mechanism in the jack which could be difficult to electrically actuate across models.

    • A more complicated idea would be to couple some sound into the microphone input, and detect that in software. However, it will have to be an oscillating signal, as the microphone bandwidth typically does not range down to DC. If you just apply a steady voltage, you would get a "click" transient when it changes, but not otherwise. Injecting an audio frequency signal would require a power source - with careful design you may be able to steal enough power from the microphone bias supply to run a small oscillator. But perhaps simpler would be to have software output an audio tone on the speaker, and have your pedal switch couple this back into the microphone through a small capacitor.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-31 06:57

    According to your last update, it looks like the pedal reacts like the button on a hands free headset. So as to say, to play, pause and skip music or pickup/hangup a call. If the basics on the API don't give you a way to get hold of it (which I doubt) then you could move down to the NDK and see if it can be catched by the native level.

    I looked it up and thought this might help you find your way :

    • Audio Hardware

    Never know...

    0 讨论(0)
  • 2021-01-31 07:04

    It's not clear if this will work without having the actual hardware in my hands, but... Don't plug the jack all the way into the socket. The tip of the jack should then connect to pin 2, left audio out, of this diagram: http://pinoutsguide.com/CellularPhones-P-W/samsung_galaxy_headset_pinout.shtml and the ring should connect to pin 3, microphone in. If you push the jack all the way in, the ground pin on the jack shorts out the microphone input and you won't detect anything - pictures at https://en.wikipedia.org/wiki/File:Photo-audiojacks.jpg show how the connections will mate.

    Play some audio out the Left channel, record it on the mic channel, and measure the amplitude. That may - if it's all wired up right - tell you the pedal position. If the far end of the potentiometer is connected to the ring of the jack, that's not going to work.

    That all sounds rather Heath Robinson. You could buy an inline 3-pin female jack socket and a 4-pin male jack plug and wire the two together to get whatever pinout you need. No electronics; just connectors and bits of wire! Or use a cable like this one: http://www.ebay.co.uk/itm/1mt-3-5mm-4-Pin-Jack-Male-Plug-4-Pole-Jack-female-Socket-Extension-Lead-Cable-/251172651017?pt=UK_Computing_Sound_Vision_Audio_Cables_Adapters&hash=item3a7b0e8009&_uhb=1 and a sharp knife and some stickytape.

    The other problem would come if the phone refuses to send audio to the jack when the jack isn't plugged fully home - there's an extra contact in the socket that detects that mechanically. But it would work if you made 3-pin to 4-pin adapter.

    Or, just cut the 3.5mm jack off your (shiny new) expression pedal. Replace it with the 4-pin connector off one of those broken phone headsets you have lying around.

    0 讨论(0)
提交回复
热议问题