I have a button and I'm trying to set the audio to the speaker when the button is clicked. My tries have been futile. Any recommendations?
我有一个按钮,当按钮被点击时,我正在尝试将音频设置为扬声器。我的尝试都是徒劳的。有什么建议吗?
speakerButton.setOnClickListener {
// Check if the speaker is currently on or off
val isSpeakerOn = audioManager.isSpeakerphoneOn
// Toggle the speakerphone state
audioManager.isSpeakerphoneOn = !isSpeakerOn
// Update the audio routing
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
// Update the speaker button icon based on the speakerphone state
if (!isSpeakerOn) {
speakerButton.setImageResource(R.drawable.speaker_cross_svgrepo_com)
} else {
speakerButton.setImageResource(R.drawable.baseline_volume_up_24)
}
}
更多回答
优秀答案推荐
in your manifest add MODIFY_AUDIO_SETTINGS
permission
and audioManager.isSpeakerphoneOn = true
should do the work!
在您的清单中添加MODIFY_AUDIO_SETTINGS权限和audioManager.isSpeakerphoneOn = true应该完成这项工作!
更多回答
I have that permission set on the Manifest but it still doesn't seem to work.
我在清单上设置了该权限,但它似乎仍然不起作用。
If you're working with audioPlayback in your application, double check audio session. audioManager can interact with audio session, so conflicts may happen.
如果您在应用程序中使用AudioPlayback,请仔细检查音频会话。AudioManager可以与音频会话交互,因此可能会发生冲突。
我是一名优秀的程序员,十分优秀!