Wednesday, 24 November 2021

How to play audio through earpiece in android 12?

I am developing a voice call app for android using PeerJS and WebView. And I want the audio to play through the earpiece. Here is my code,

private fun initAudio(){
  am = getSystemService(AUDIO_SERVICE) as AudioManager
  volumeControlStream = AudioManager.STREAM_VOICE_CALL
  am.mode = AudioManager.MODE_IN_COMMUNICATION
  am.isSpeakerphoneOn = false//<= not working in android 12
}

private fun toggleSpeakerMode(){
  am.isSpeakerphoneOn = !am.isSpeakerphoneOn // <= final value is always true in android 12
}

The above code works fine on older versions of android, but not in android 12 (emulator). am.isSpeakerphoneOn is always true in android 12. Am I doing something wrong here? Or is it a bug in the emulator?



from How to play audio through earpiece in android 12?

No comments:

Post a Comment