简体   繁体   中英

How to detect use the STREAM_VOICE_CALL or the audio speaker in android?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM