音频输出广播和强制路由 - Android

发布于 2025-02-10 09:35:48 字数 1789 浏览 1 评论 0原文

我正在尝试建立一个广播,即每次更改音频输出路线时,都会将其路由到选择的设备(无论是在呼叫还是在播放媒体/音乐时)。

例如:当接听插入/蓝牙耳机的呼叫时,用户决定单击手机上的扬声器图标时,我需要该应用程序将其路由回到耳机。

我成功地更改了A 有线耳机上的音频输出,单击一个按下以下代码的按钮:

mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(false);

以及带有以下蓝牙耳机(不在呼叫中,仅在媒体/音乐上工作):

mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);

我有两个问题:

  1. 当我单击呼叫屏幕中的扬声器图标以停止使用我的蓝牙耳机,然后单击我的应用程序上的按钮以用上述代码重新启动通往我的蓝牙耳机的路由,它不起作用:(
  2. didn'' t设法找到一种播放音频输出更改的方法,以使上述代码自动工作,而无需按下我的应用程序的按钮。甚至不要在蓝牙耳机上为我
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
                switch (state) {
                    case BluetoothHeadset.STATE_AUDIO_CONNECTED:
                        Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_SHORT).show();
                    case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                        Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
                        mAudioManager.setMode(AudioManager.MODE_IN_CALL);
                        mAudioManager.startBluetoothSco();
                        mAudioManager.setBluetoothScoOn(true);
                        mAudioManager.setSpeakerphoneOn(false);
                        break;
                }
            }
        }
    };

服务

I'm trying to build a broadcast that every time audio output route is changed it would route it to a device of choice instead (both in calls and when playing media/music).

for example: when getting a call with plugged/Bluetooth headset and then the user decides to click on the speaker icon on the phone, I need the app to route it back to the headset.

I succeeded to change the audio output on a WIRED headset with a click of a button with following code:

mAudioManager.setMode(AudioManager.MODE_IN_CALL);
mAudioManager.setSpeakerphoneOn(false);

and on a Bluetooth headset with the following (not in calls, works on media/music only):

mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setSpeakerphoneOn(false);

I have two problems:

  1. When I click on the speaker icon in the call screen to stop using my Bluetooth headset and then click the button on my app to redirct the route to my Bluetooth headset with the above code it doesn't work :(
  2. Didn't manage to find a way to broadcast audio output change in order for the above code to work automatically without a need to press my app's button. only came across the following code that only suppose to work on a Bluetooth headset and not plugged headset and it's don't even work for me on the Bluetooth headset.
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
                int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
                switch (state) {
                    case BluetoothHeadset.STATE_AUDIO_CONNECTED:
                        Toast.makeText(context, "Bluetooth Connected", Toast.LENGTH_SHORT).show();
                    case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                        Toast.makeText(context, "Bluetooth Disconnected", Toast.LENGTH_SHORT).show();
                        mAudioManager.setMode(AudioManager.MODE_IN_CALL);
                        mAudioManager.startBluetoothSco();
                        mAudioManager.setBluetoothScoOn(true);
                        mAudioManager.setSpeakerphoneOn(false);
                        break;
                }
            }
        }
    };

Thanks so much in advance for the help!

any answer would help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文