Android - Play audio from earpiece

play music through earpiece and speaker android
change audio output android
android audio routing
how to play audio file in android programmatically
use earpiece as second speaker android no root
test ear speaker android
android audio settings
android set media player volume programmatically

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);  
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL); 
m_amAudioManager.setSpeakerphoneOn(false); 

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

mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();

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.

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

Play through the ear piece

mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

Play through the speaker phone

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

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.

Stealth Audio Player, Usually, there are two ways of playing music on Android device. You can either use your headphones or a speaker. But how about a third option? The purpose  Plays any kind of Audio from the earpiece of your device. Plays Videos on screen and their audio is routed through the earpiece. Folders Browser lets you locate specific files. Ignore Folders

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(); 
    this.player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

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

All audio thru ear speaker, I'lI then notice that the sound is playing thru the ear speaker that you would listen to while making a call on the head set. Its not just for videos  A new app from developer Usman Farhat uses your phone's earpiece speaker to play audio, and the end result, as the XDA Portal team has pointed out, is a subtle, private listening experience. Since this speaker is normally used for phone calls rather than pumping out the jams, it's easy to direct the sound in a way that only you can hear.

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);
 mAudioManager.setMode(AudioManager.STREAM_MUSIC);

3- set audio to play from earpiece:

mAudioManager.setSpeakerphoneOn(false);

4- play:

mPlayer.setLooping(true);
mPlayer.start();

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!

Play Music Through Your Phone's Earpiece Speaker for a Low-Key , Install Stealth Audio Player for free from the Google Play Store it'll immediately start playing through your earpiece speaker at a volume that wouldn't be Don't Miss: The 8 Best Local & Streaming Music Players for Android. There are 2 important pieces required to stream it through the earpiece. Certain devices and Android versions only require 1 of the 2, but using both seems to be the best bet. The first is to use the AudioManager service from the current Context and set SpeakerphoneOn to false as well as set the Mode to Mode.InCall.

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

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

    mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    mAudioManager.setSpeakerphoneOn(false);

Is it possible to change which output the audio is played through , You can simply use an app like Earpiece. It does what you want to do. You can pop Earpiece up on top of another application simply by holding down the  Using APKPure App to upgrade Stealth Audio Player - play audio through earpiece, fast, free and save your internet data. The description of Stealth Audio Player - play audio through earpiece Usually, there are two ways of playing music on Android device. You can either use your headphones or a speaker.

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("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse",
    int.class,
    int.class);
// 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.

Phone sound coming from earpiece? Easy Fix (Still work in 2020 , Usually, there are two ways of playing music on Android device. You can either use your headphones or a speaker. But how about a third  Listen to audio books and music through your phone's earpiece when you don't have earphones handy. Boost your audio (Android 2.3 through 4.1 or so only, not working on 4.2.1 or higher), whether

Play Music through earpiece! (android only), Description When creating the room with Android SDK, the RN app sets the audio device to EARPIECE instead of SPEAKER, but the UI shows  Hi Christopher, I currently use this code to do it and it works in 1.5 audio_service.setSpeakerphoneOn(false); audio_service.setMode(AudioManager.MODE_IN_CALL); audio_service.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL); However its broken from 1.6 on and I cant find the new solution.

Use Your Android Earpiece As Speakers For Playing Music, I have my phone connected to a Bluetooth speaker and the headphones plugged in. Now I’d like to play audio through the Bluetooth speaker. When I set the audio stream to AudioManager.STREAM_MUSIC it just plays over the headphones. It doesn’t matter if it plays on the headphones as well but I need it to play on the Bluetooth speaker.

Play Music Through Your Phone's Earpiece With Stealth Audio Player, Note: You can play back the audio data only to the standard output device. Currently, that is the mobile device speaker or a Bluetooth headset. You cannot play sound files in the conversation audio during a call. The basics. The following classes are used to play sound and video in the Android framework: MediaPlayer

Comments
  • Please check my answer here: stackoverflow.com/a/29881184/672773 Hope its helps :)
  • I'll have to try that out. I've been using the solution from the post you referenced for years without real issue. I have had a few random complaints of weird changes to audio in other apps, but they could never be confirmed. I will check this out, it certainly sounds like a better solution. I never liked how my solution looked, even through it worked.
  • Specifically, some versions of firmware running on Samsung Galaxy S2 and Micromax run into trouble with the solution in the other post. If you're still using it, I'd suggest moving to this code fragment. It has not failed me thus far.
  • It appears with this approach you need to re-"prepare" the MedialPlayer when switching modes. Is that your experience as well?
  • Yes I had to reset and re-prepare the media player. Thanks for pointing that out. I will update the answer to reflect this.
  • Deepak Bala> Please see a related issue: stackoverflow.com/questions/33062660/… , now i need to play the audio received and record audio from microphone at the same time. If i set MODE_IN_CALL audio is recorded clearly, but speaker goes off. What now (Micromax tablet)?
  • The line mAudioManager.setMode() has a wrong enum as input. It does work though, because the enum-value is the same as the correct enum. As the documentation states: link The SetMode method has 5 possible enumvalues: MODE_NORMAL, MODE_RINGTONE, MODE_IN_CALL, MODE_IN_COMMUNICATION The correct one here should be MODE_IN_CALL which is the same enumvalue as AudioManager.STREAM_MUSIC ;)
  • Can you tell what R.raw.skype_call_dialing reffer too in the above example ? I guess it is audio file in raw folder of project. What format should be for the file like mp3, aac?
  • The phone is able to play from the earpiece on other apps. I'm not able to get it work on my app alone.
  • @Gaffi: Sounds like you're getting compile time errors rather than runtime errors. Maybe you haven't imported the reflection package? If you use Eclipse it should automatically offer suggestions on how to resolve those issues.