Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Play audio from earpiece

Tags:

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.

like image 760
Deepak Bala Avatar asked Dec 19 '12 20:12

Deepak Bala


People also ask

How do I play music through my headphone jack?

Plug in your headphones, and adjust any inline volume or power controls so that you should be able to hear any sound that is sent to them. Right-click the volume control icon in the system tray. Select "Playback Devices". Click on the volume control icon again, and adjust the volume so that it triggers a sound event.

How do I test my earpiece on Android?

Android Code for Audio Test Just dial *#*#0673#*#* OR *#*#0289#*#* for Audio test.


1 Answers

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.

like image 138
Deepak Bala Avatar answered Sep 21 '22 09:09

Deepak Bala