是否可以通过带耳机的耳机播放音调?

Is it possible to play a tone via the Ear Speaker with Headphone in?

我正在制作一个 android 应用程序来测试手机,我需要能够在插入耳机时通过耳机扬声器播放音调。我能够通过耳朵播放音调未插入耳机时扬声器,但插入耳机时则不会。插入耳机时是否可以通过耳机播放我的音调?我将包括在没有耳机的情况下工作的代码。

    mp = MediaPlayer.create(getApplicationContext(), R.raw.tone_1ghz_5sec);
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    //set to false for through headphones
    audioManager.setSpeakerphoneOn(false);
    mp.start();

在其他活动中,我使用了音频系统class强制扬声器,这是这里的关键吗?

我能够让它工作。你可以让它通过耳机扬声器,或者调用扬声器,无论你想通过上面的代码调用它。如果你想让它仍然通过通话扬声器播放,即使插入耳机,你也可以这样做:

   AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    Class<?> cl = null;
    try {
        cl = Class.forName("android.media.AudioManager");
        setDeviceConnectionState = cl.getMethod("setWiredDeviceConnectionState", new Class[] {
                Integer.TYPE, Integer.TYPE, String.class });
        //4 is for the headset with microphone 8 is for headset without microphone
        //0 is for off and 1 is for on
        setDeviceConnectionState.invoke(manager, new Object[] { 4 , 0, "device" });
        setDeviceConnectionState.invoke(manager, new Object[] { 8 , 0, "device" });
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }