连接耳机时如何将默认音频路由到听筒? [英] How to route default audio to ear piece when headphones are connected?

查看:136
本文介绍了连接耳机时如何将默认音频路由到听筒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我们只需要将耳机插孔用作按钮即可.

I am developing an app in which we need to use the headphone jack as a button only.

要求:连接耳机时(无需通过耳机播放音频)通过听筒播放默认音频(通话)

Requirement : Play the default audio (calling) via earpiece when headsets are connected (no need of audio through headphones)

有许多通过扬声器和耳机以及蓝牙耳机路由音频的示例,但是如果连接了耳机,则没有通过设备的耳麦路由音频的例子.我已经尝试了很多,有些链接是

There are many example of routing audio through speaker and headphones and also bluetooth headsets but nothing about routing the audio through ear speakers of devices if headsets are connected. I have tried a lot and some links are

Android:强制音频路由(不适用于我的情况)

Android : Force audio routing (not working in my scenario)

我已经检查了SoundAbout(https://play.google.com/store/apps/details?id=com.woodslink.android.wiredheadphoneroutingfix&hl=zh_CN )应用程序,它将音频路由到各种端口,例如耳机,扬声器和听筒.

I have checked SoundAbout(https://play.google.com/store/apps/details?id=com.woodslink.android.wiredheadphoneroutingfix&hl=en) app and it is routing the audio to various port like headset, speakers and earpieces.

如果连接了头戴式耳机,我已经可以听到扬声器的音频了:这是我的代码

I have got audio to speakers if headsets are connected: Here is my code

if (Build.VERSION.SDK_INT >= 21) {
            ForegroundService.audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
            ForegroundService.audioManager.setSpeakerphoneOn(true);
            SplashScreen.preferences.edit().putBoolean("isKey", true).commit();
        } else {
            Class audioSystemClass = null;
            try {
                audioSystemClass = Class.forName("android.media.AudioSystem");
                Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
                setForceUse.invoke(null, FOR_MEDIA, FORCE_SPEAKER);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }


            SplashScreen.preferences.edit().putBoolean("isKey", true).commit();
            ForegroundService.audioManager.setSpeakerphoneOn(true);
        }

推荐答案

经过大量研究,我发现如果不使用反射就无法实现这种功能.首先,您需要插入耳机插孔,然后使用合适的参数调用setWiredDeviceConnectionState()方法,然后其行为就像断开了耳机一样,但是单击仍然有效.因此,这是一个hack,但是按照我的要求,它不是万无一失的解决方案,但可以立即使用.这是我执行此操作的代码,

After researching a lot, I found it out that there is not any way to achieve this funtionality without using reflection. First you need to put headset jack in and then call the method setWiredDeviceConnectionState() with suitable parameters then it behave like the headphone are disconnected but click works still. So it is a hack but as per my requirement, it's not a foolproof solution but working for now. Here is my code to do this,

private void sendIntent(Intent i) {
        Method m;
        Log.i(TAG, "Device sdk = " + Build.VERSION.SDK_INT);
        try {
            if (Build.VERSION.SDK_INT < 16) {
                Class<?> clazz = Class.forName("android.app.ActivityManagerNative");
                m = clazz.getMethod("broadcastStickyIntent", Intent.class, String.class);
                m.setAccessible(true);
                m.invoke(clazz, i, null);
                return;
            } else if (Build.VERSION.SDK_INT < 23) {
                //int type, int state, String address, String name
                m = am.getClass().getMethod("setWiredDeviceConnectionState", Integer.TYPE, Integer.TYPE, String.class);
                m.setAccessible(true);
                Object[] objArr = new Object[3];
                objArr[0] = (i.getIntExtra("microphone", 0) == 0) ? 8 : 4;
                objArr[1] = i.getIntExtra("state", 0);
                objArr[2] = i.getStringExtra("name");
                m.invoke(am, objArr);
            } else {
                //int type, int state, String address, String name
                m = am.getClass().getMethod("setWiredDeviceConnectionState", Integer.TYPE, Integer.TYPE, String.class, String.class);
                m.setAccessible(true);
                Object[] objArr = new Object[4];
                objArr[0] = (i.getIntExtra("microphone", 0) == 0) ? 8 : 4;
                objArr[1] = i.getIntExtra("state", 0);
                objArr[2] = i.getStringExtra("address");
                objArr[3] = i.getStringExtra("name");
                m.invoke(am, objArr);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

发送意图:

@TargetApi(Build.VERSION_CODES.M)
public class HeadSetJackReciever extends AudioDeviceCallback {
    public static boolean isAudioChecked;

    public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        if (addedDevices.length != 0) {
            for (int i = 0; i < addedDevices.length; i++) {
                if (addedDevices[i].getType() == AudioDeviceInfo.TYPE_WIRED_HEADSET) {
                    AudioDeviceInfo audioDeviceInfo = addedDevices[i];
                    int microphone = audioDeviceInfo.getType();
                    String headsetName = "DCS";
                    String headsetAddress = "";
                    try {
                        Method method = audioDeviceInfo.getClass().getMethod("getAddress");
                        method.setAccessible(true);
                        headsetAddress = (String) method.invoke(audioDeviceInfo);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    Log.e("TEST", "microphone:"+microphone);
                    Log.e("TEST", "headsetName:"+headsetName);
                    Log.e("TEST", "headsetAddress:"+headsetAddress );
                    Intent intent = new Intent(ForegroundService.context, SelectAudioOutput.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("microphone",microphone);
                    intent.putExtra("headsetName",headsetName);
                    intent.putExtra("headsetAddress",headsetAddress);
                    ForegroundService.context.startActivity(intent);
                }
            }
        }
    }

    public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
        if (removedDevices.length != 0) {
            Log.e("TEST", "Audio deinserted");
            if (SplashScreen.preferences.getBoolean("isKey", false)) {
                Intent startIntent = new Intent(ForegroundService.context, ForegroundService.class);
                startIntent.setAction(Constants.ACTION.STARTNOTIFICATION_ACTION);
                ForegroundService.context.startService(startIntent);
            } else {
                Intent startIntent = new Intent(ForegroundService.context, ForegroundService.class);
                startIntent.setAction(Constants.ACTION.STOPNOTIFICATION_ACTION);
                ForegroundService.context.startService(startIntent);
            }
            ForegroundService.audioManager.setMode(AudioManager.MODE_IN_CALL);
            ForegroundService.audioManager.setSpeakerphoneOn(false);
        }
    }
}

对于Lollipop及更低版本:

for Lollipop and lower versions :

if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            headsetName = intent.getStringExtra("name");
            microphone = intent.getIntExtra("microphone", 0);

            int state = intent.getIntExtra("state", -1);
            switch (state) {
                case 0:
                    Log.d("onReceive", "Headset unplugged");
                    Log.e("TEST", "Audio deinserted");
                    if (SplashScreen.preferences.getBoolean("isKey", false)) {
                        Intent startIntent = new Intent(ForegroundService.context, ForegroundService.class);
                        startIntent.setAction(Constants.ACTION.STARTNOTIFICATION_ACTION);
                        context.startService(startIntent);
                    } else {
                        Intent startIntent = new Intent(ForegroundService.context, ForegroundService.class);
                        startIntent.setAction(Constants.ACTION.STOPNOTIFICATION_ACTION);
                        context.startService(startIntent);
                    }
                    ForegroundService.audioManager.setMode(AudioManager.MODE_IN_CALL);
                    ForegroundService.audioManager.setSpeakerphoneOn(false);
                    break;
                case 1:

                    Log.d("onReceive", "Headset plugged");
                    Log.e("TEST", "microphone:"+microphone);
                    Log.e("TEST", "headsetName:"+headsetName);
                    Log.e("TEST", "headsetAddress:"+headsetAddress );
                    Intent intentone = new Intent(ForegroundService.context, SelectAudioOutput.class);
                    intentone.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intentone.putExtra("microphone",microphone);
                    intentone.putExtra("headsetName",headsetName);
                    intentone.putExtra("headsetAddress",headsetAddress);
                    context.startActivity(intentone);
                    break;
            }
        }

让我知道我是否想念一些东西.谢谢.

Let me know if I miss something. Thanks.

这篇关于连接耳机时如何将默认音频路由到听筒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆