还有其他方法可以禁用耳机吗?

时间:2017-01-13 14:54:50

标签: android android-audiomanager

我使用了AudioManager方法:setWiredDeviceConnectionState来禁用我设备上的耳机。它工作得很好,直到我遇到一些较新的模型,现在它没有工作。我认为这是因为Hack不适用于这些模型。是否有任何升级到以前的黑客或任何其他方式从设备禁用耳机?我需要能够在插入耳机时通过In-Call_Speaker播放音调。请帮助!!!

这是我在运行活动时提供的例外:

W/System: ClassLoader referenced unknown path: /data/app/com.example.raptorroboticstest-2/lib/arm64
W/System.err: java.lang.NoSuchMethodException: setWiredDeviceConnectionState [int, int, class java.lang.String]

这是禁用/启用耳机的方法:

    AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    try {
        Class<?> cl = Class.forName("android.media.AudioManager");
        setWiredDeviceConnectionState = 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
        setWiredDeviceConnectionState.invoke(manager, new Object[]{4, 0, "device"});
        setWiredDeviceConnectionState.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();
    }

1 个答案:

答案 0 :(得分:0)

旧问题,但搜索方法名称时排名最高的结果之一。

方法签名已更改。在棉花糖及更高版本上,还有一个额外的“地址”参数,该参数位于现有名称String参数之前。

蓝牙和USB设备使用address参数,但是空字符串足以满足有线minijack耳机的要求。

Lollipop and below

  

public void setWiredDeviceConnectionState(int设备,整数状态,字符串名称)

Marshmallow and later

  

public void setWiredDeviceConnectionState(int类型,int状态,字符串地址,字符串名称)

在使用Android Pie之前,您可以直接在AudioSystem上调用setDeviceConnectionState(),如果您需要在棉花糖之前指定蓝牙/ USB设备的地址,则必须执行此操作。像AudioManager方法一样,其签名从棉花糖adding the name String parameter开始更改,而在only included the address之前更改。

相关问题