简体   繁体   中英

How to detect if the audio is currently playing on an Android phone speaker?

I need to have some custom behavior in my app when the audio playback is done on the device speaker (not a wired headphone and not a BlueTooth receiver). To do so I'm doing the following

    AudioManager audioManager = (AudioManager) service.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    isSpeakerPhoneOn = audioManager.isSpeakerphoneOn();

But isSpeakerPhoneOn is always false. By the way, I'm calling the code after playback is started.

Any idea what I'm doing wrong?

I suggest you to try this

AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
 {
     // do something - or don't
 }

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