Android11上实现独立切换MIC和扬声器

最近处理了一个需求,就是控制系统选择的麦克风和扬声器。原生的逻辑当插上一个既是麦克风又是扬声器的设备时,会去同时切换,与我们的需求不符。

原生逻辑看以下的代码

frameworks\base\services\usb\java\com\android\server\usb\UsbAlsaDevice.java

 核心的代码如下:

    /** Updates AudioService with the connection state of the alsaDevice.
     *  Checks ALSA Jack state for inputs and outputs before reporting.
     */
    public synchronized void updateWiredDeviceConnectionState(boolean enable) {
        if (!mSelected) {
            Slog.e(TAG, "updateWiredDeviceConnectionState on unselected AlsaDevice!");
            return;
        }
        String alsaCardDeviceString = getAlsaCardDeviceString();
        if (alsaCardDeviceString == null) {
            return;
        }
        try {
            // Output Device
            if (mHasOutput) {
                int device = mIsOutputHeadset
                        ? AudioSystem.DEVICE_OUT_USB_HEADSET
                        : AudioSystem.DEVICE_OUT_USB_DEVICE;
                if (DEBUG) {
                    Slog.d(TAG, "pre-call device:0x" + Integer.toHexString(device)
                            + " addr:" + alsaCardDeviceString
                            + " name:" + mDeviceName);
                }
                boolean connected = isOutputJackConnected();
                Slog.i(TAG, "OUTPUT JACK connected: " + connected);
                int outputState = (enable && connected) ? 1 : 0;
                if (outputState != mOutputState) {
                    mOutputState = outputState;
                    mAudioService.setWiredDeviceConnectionState(device, outputState,
                                                                alsaCardDeviceString,
                                                                mDeviceName, TAG);
                }
            }

            // Input Device
            if (mHasInput) {
                int device = mIsInputHeadset ? AudioSystem.DEVICE_IN_USB_HEADSET
                        : AudioSystem.DEVICE_IN_USB_DEVICE;
                boolean connected = isInputJackConnected();
                Slog.i(TAG, "INPUT JACK connected: " + connected);
                int inputState = (enable && connected) ? 1 : 0;
                if (inputState != mInputState) {
                    mInputState = inputState;
                    mAudioService.setWiredDeviceConnectionState(
                            device, inputState, alsaCardDeviceString,
                            mDeviceName, TAG);
                }
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "RemoteException in setWiredDeviceConnectionState");
        }
    }

可以看到原生的代码其实是有对输入设备和输出设备有分开切换的,只不过系统合并成了一个方法。这样我们的思路就出来了,我们单独摘出来做处理就行了,还是这个文件。

	private boolean mSelectedInput = false;
    private boolean mSelectedOutput = false;

	public synchronized void startInput() {
        mSelectedInput = true;
        mInputState = 0;
        startJackDetect();
        updateWiredDeviceConnectionStateInput(true);
    }

    public synchronized void startOutput() {
        mSelectedOutput = true;
        mOutputState = 0;
        startJackDetect();
        updateWiredDeviceConnectionStateOutput(true);
    }

    public synchronized void stopInput() {
        if (!mSelectedOutput) {
            stopJackDetect();
        }
        updateWiredDeviceConnectionStateInput(false);
        mSelectedInput = false;
    }

    public synchronized void stopOutput() {
        if (!mSelectedInput) {
            stopJackDetect();
        }
        updateWiredDeviceConnectionStateOutput(false);
        mSelectedOutput = false;
    }

	 public synchronized void updateWiredDeviceConnectionStateOutput(boolean enable) {
        if (!mSelectedOutput) {
            Slog.e(TAG, "updateWiredDeviceConnectionStateOutput on unselected AlsaDevice!");
            return;
        }
        String alsaCardDeviceString = getAlsaCardDeviceString();
        if (alsaCardDeviceString == null) {
            return;
        }
        try {
            // Output Device
            if (mHasOutput) {
                int device = mIsOutputHeadset
                        ? AudioSystem.DEVICE_OUT_USB_HEADSET
                        : AudioSystem.DEVICE_OUT_USB_DEVICE;
                if (DEBUG) {
                    Slog.d(TAG, "pre-call device:0x" + Integer.toHexString(device)
                            + " addr:" + alsaCardDeviceString
                            + " name:" + mDeviceName);
                }
                boolean connected = isOutputJackConnected();
                Slog.i(TAG, "OUTPUT JACK connected: " + connected);
                int outputState = (enable && connected) ? 1 : 0;
                if (outputState != mOutputState) {
                    mOutputState = outputState;
                    mAudioService.setWiredDeviceConnectionState(device, outputState,
                                                                alsaCardDeviceString,
                                                                mDeviceName, TAG);
                }
            }
        } catch (RemoteException e) {
            Slog.e(TAG, "RemoteE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值