0

Take audio play in whatsapp as example, if your ear near the phone, the speaker will change to the earphone speaker, while the ear leave the phone, it use the audio speaker.

I am using the mediaplayer to play the audio file, and found that there is

 setAudioStreamType(AudioManager.STREAM_VOICE_CALL); 

to switch the speaker to the ear speaker. and

 setAudioStreamType(AudioManager.STREAM_MUSIC)

for default audio speaker.

The problem is , how to detect when the ear is near the phone, or is it necessary to implement by my program (should have some default android API for that ? )

Thanks a lot for helping

user782104
  • 13,233
  • 55
  • 172
  • 312

1 Answers1

2

Most (or may be all?) android devices have proximity sensor. I think it is what you need.
Check this question and Android documentation on Sensor for more details.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Konstantin
  • 174
  • 7