audioManager 不适用于 Android 11 和 12

发布于 2025-01-15 23:38:31 字数 895 浏览 5 评论 0原文

我正在制作一个内部呼叫应用程序,当我拨打一个号码时,本机 Android 拨号器会打开,然后我不会跟随本机拨号器中的呼叫,而是跟随应用程序中的呼叫。问题是,当我想激活或停用本机拨号器扬声器时,从应用程序中它没有任何效果。本机拨号器位于后台,应用程序位于前台,但由于某种原因,当我尝试从应用程序激活扬声器以跟随此处的呼叫时,它不起作用。这种情况仅发生在 Android 11 和 12 的版本中,但是在 Android 8,9 和 10 的版本中它工作得很好,我可以通过应用程序操作扬声器,而无需使用本机拨号器。

这是我激活和停用扬声器的代码:

private fun enableSpeaker() {
        audioManager?.let {
            if (!it.isSpeakerphoneOn) {
                audioManager!!.mode = AudioManager.MODE_IN_COMMUNICATION
                audioManager!!.isSpeakerphoneOn = true
            }
        }
    }
private fun disableSpeaker() {
        audioManager?.let {
            if (it.isSpeakerphoneOn) {
                //audioManager.setMicrophoneMute(false)
                audioManager!!.mode = AudioManager.MODE_NORMAL
                audioManager!!.isSpeakerphoneOn = false
            }
        }
    }

I am making an internal calls application, when I call a number, the native android dialer opens and then instead of following the call in the native dialer, I follow the call in the app. The problem is that from the app it has no effect when I want to activate or deactivate the native dialer speaker. The native dialer is in the background, and the app is in the foreground, but for some reason when I try to activate the speaker from the app, to follow the call from here, it doesn't work. This only happens in versions of Android 11 and 12, however in versions of Android 8,9 and 10 it works perfectly, I can manipulate the speaker from the app, without going to the native dialer.

This is my code to activate and desactivate the speaker:

private fun enableSpeaker() {
        audioManager?.let {
            if (!it.isSpeakerphoneOn) {
                audioManager!!.mode = AudioManager.MODE_IN_COMMUNICATION
                audioManager!!.isSpeakerphoneOn = true
            }
        }
    }
private fun disableSpeaker() {
        audioManager?.let {
            if (it.isSpeakerphoneOn) {
                //audioManager.setMicrophoneMute(false)
                audioManager!!.mode = AudioManager.MODE_NORMAL
                audioManager!!.isSpeakerphoneOn = false
            }
        }
    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…红帽 2025-01-22 23:38:31

对于 Android 12 及更高版本,请使用 audioManager.setCommunicationDevice 而不是 audioManager.isSpeakerPhoneOn = true

更多信息:https://developer.android.com/reference/kotlin/android/media/AudioManager#setCommunicationDevice(android.media.AudioDeviceInfo)

For Android 12 and above, use audioManager.setCommunicationDevice instead of audioManager.isSpeakerPhoneOn = true

More info here : https://developer.android.com/reference/kotlin/android/media/AudioManager#setCommunicationDevice(android.media.AudioDeviceInfo)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文