Android – Play audio from earpiece-ThrowExceptions

Exception or error:

I’d like to play audio from a file into the phone’s earpiece (or headset of that is connected) using the android MediaPlayer. I tried using the MODE_IN_CALL hack as suggested in this thread – Android – Getting audio to play through earpiece but that did not help. The app also has the android.permission.MODIFY_AUDIO_SETTINGS permission, but any call to m_amAudioManager.setSpeakerphoneOn(false); is ignored.

private AudioManager m_amAudioManager;  
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);  

A call to setMode(AudioManager.STREAM_VOICE_CALL); on the MediaPlayer did not help either.


Has something changed since android 2.3 to break this code ? I tried the code on Android ICS without any success. Can someone point me in the right direction ? The audio always plays from the speaker phone so far.

How to solve:

It turns out the right way to do this is through the following code.

Play through the ear piece


Play through the speaker phone


That is it. Any other solution (Including the one posted here -> Android – Getting audio to play through earpiece) does not work consistently across devices. In fact using MODE_IN_CALL ensures that your audio will never play on certain devices.

Note: You will need to re-prepare the media player to change streams.


i just wanted to add a complete example for Deepak Bala’s Answer. It works when not using the MediaPlayer.create() method:

    this.player = new MediaPlayer(); 

    try {
        this.player.setDataSource(getContext(), Uri.fromFile(new File(m.localFileUrl)));
    } catch (IllegalStateException e1) {
        // TODO Auto-generated catch block
    } catch (IOException e1) {
        // TODO Auto-generated catch block


The following code works fine for me and I have tested in on multiple devices:

1- creating instances:

MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.skype_call_dialing);
AudioManager mAudioManager = ((AudioManager).getSystemService(Context.AUDIO_SERVICE));

2- if you want to set volume to max (optional)

 int maxVolumeMusic = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
 mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolumeMusic, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

3- set audio to play from earpiece:


4- play:


Done!!!! But there is some tips I am going to share with you:

1- in step 2, if you set Audiomanager.setMode() before AudioManager.setStreamVolume() then the volume would not set to max!

2- if you add mPlayer.prepare() before mPlayer.setLooping(true); it would lead to crash!, you should not use mPlayer.prepare() while mPlayer is initializing like this:

MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.skype_call_dialing);

3- in step 2, mAudioManager.setMode() I used AudioManager.STREAM_MUSIC, when I try other modes, in many cases I was not able to switch on and off speaker!


Ok in my case the issue was solved by using the below code

    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);



How about this then:

// Make sure that streams following the PHONE routing strategy aren't forced
// to play in the speaker.
Class audioSystemClass = Class.forName("");
Method setForceUse = audioSystemClass.getMethod("setForceUse",
// First 0 == FOR_COMMUNICATION, second 0 == FORCE_NONE.
setForceUse.invoke(null, 0, 0);

Then use STREAM_VOICE_CALL for your MediaPlayer.

If the setForceUse call succeeds and the audio from your MediaPlayer still plays in the loudspeaker, then I’d say something is messed up in the AudioPolicyManager implementation on the phone/tablet that you’re testing this on.


I found i needed to set both the mediaplayer and Audiomanager

private MediaPlayer mp = new MediaPlayer();
AudioManager mAudioManager = (AudioManager) v.getContext()


This method is working fine for me.

You need to set audio manager mode too. and then using audiomgr.setSpeakerphoneOn(false) api you can toggle.

AudioManager audiomgr = (AudioManager) 

Leave a Reply

Your email address will not be published. Required fields are marked *