Search code examples
androidaudiowebviewwebrtcpeerjs

WebView change audio output device


How can I change the audio output of WebView from loudspeaker to earpiece? I tried this code, but it is not working

    private fun setupAudio() {
        val am = getSystemService(AUDIO_SERVICE) as AudioManager
        am.mode = AudioManager.MODE_IN_COMMUNICATION
        am.stopBluetoothSco()
        am.isSpeakerphoneOn=false
        Log.e("Foo","Current mode is ${am.mode}")
    }

Have permission

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

Solution

  • I got it working, the trick is to call setupAudio() function after the playback starts.