5

Is it possible to play audio trough the phone speaker of an android device? The smaller speaker inside a phone that produces a low volume sound which can only be heard when listing closely with your ear against the phone.

Hopefully my description is clear enough to understand my question.

If it is possible, a example for how to accomplish this would be really helpful.

EDIT Currently i'm using the following code to initialize my MediaPlayer.

 mediaPlayer = new MediaPlayer();
 mediaPlayer.setOnBufferingUpdateListener(this); 
 mediaPlayer.setOnCompletionListener(this);
 mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
 mediaPlayer.setDataSource(audioPath);
 mediaPlayer.prepare();
Luciano
  • 2,691
  • 4
  • 38
  • 64

1 Answers1

7

If you want to play audio through the earpiece, use the STREAM_VOICE_CALL stream type. If you use the MediaPlayer class there's a setAudioStreamType method that you can use for this purpose, and for the AudioTrack class you pass the stream type to the constructor.

Michael
  • 57,169
  • 9
  • 80
  • 125
  • Do i need to use that AudioTrack class? Because at the moment i set the audio source by calling setDataSource on the MediaPlayer and pass the filepath of my mp3 file. And when i call setAudioStreamType on the MediaPlayer, nothing happends. I will edit my question with the code is currently use to play audio. – Luciano Nov 05 '13 at 09:26
  • Thnx, for your answer. I needed to call audioManager.setSpeakerphoneOn(false); in combination with setAudioStreamType to make it work. – Luciano Nov 05 '13 at 09:50
  • No, you don't need to use `AudioTrack`. I just mentioned it since I didn't know what you were using. – Michael Nov 06 '13 at 08:28