Question

Which Android API can we use to set the volume level on my tablet for playing a tone when the headset is plugged in . Currently I use the following API which does not seem to work when the headset is plugged in.

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 9, AudioManager.FLAG_SHOW_UI);

I made the following settings to route audio through the speaker when the headset is plugged in (this is the requirement of my app) audioManager.setMode(AudioManager.MODE_IN_CALL); audioManager.setSpeakerphoneOn(true);

When I play my desired tone after this setting the audio is routed through the speaker (even though headset is connected) but the volume is too high , which looks like the setStreamVolume API does not work since I have set it to only volume level 9 which is pretty low. If I remove the headset and run my use-case then this API works fine, i.e the tone is played at a lower volume.

Is there any other API that can be used to set the volume for playing tones in Android when the headset is plugged in ?

Was it helpful?

Solution

Unless your app is a cellular call replacement, using MODE_IN_CALL actually optimizes cellular audio and starves STREAM_MUSIC on resource. If you are using STREAM_MUSIC audio with MODE_IN_CALL, depending on phone manufacturer/carrier extension of AudioPolicyManagerBase, you will see many odd behavior such as maybe the case you are seeing.

First, make sure you specify playing audio over STREAM_MUSIC. So that your volume control indeed is controlling the right stream.

Second, I would suggest use MODE_IN_COMMUNICATION instead of MODE_IN_CALL. Many manufacturer/carrier specific audio policy is specific to MODE_IN_CALL(cellular call) only.

Third, if it still happens, you should test your app on a different phone manufacturer/carrier to isolate the issue. Sometime it is a device specific flaw that you just can't fix. I have seen some cases like this myself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top