简体   繁体   中英

audioManager.setSpeakerphoneOn(true) is affecting all the activities in android project

I wanted to use the speaker outlet of android even though a headphone is connected (only for one activity) so the code I wrote is:

AudioManager audioManager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setMode(AudioManager.STREAM_MUSIC);
        audioManager.setSpeakerphoneOn(true);

in the oncreate() method of that activity and it worked but in another activity I don't want the same thing to happen. I want the sound to be played in the headphone if headphone is connected and through speaker if headphone is not connected but even in the other activity the sound is played only on speaker even though headphone is connected. Can someone help and tell me where am I going wrong.

Just set
audioManager.setMode(AudioManager.MODE_NORMAL); audioManager.setSpeakerphoneOn(false);
inside the onDestroy() method of this activity.

Update
This answer seems to solve the same problem.

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