Is there a way in Android to force output through the phone speaker, even if a headphone is plugged into the jack? I know there has to be a way because when you are talking on the phone, you can put someone on speaker phone, even if there is headphones plugged into the jack.
You can change this system-wide using the AudioManager.setSpeakerphoneOn
method.
I don't believe you can set this for a particular MediaPlayer
/AudioTrack
/SoundPool
instance, but depending on your use case, you might actually be looking to set your audio stream type using MediaPlayer.setAudioStreamType
or an equivalent for other audio playback classes.
Sound output through speaker
AudioManager m_amAudioManager =
AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_NORMAL);
m_amAudioManager.setSpeakerphoneOn(true);
This worked for me
First, I Added permissions in Manifest.xml
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Then, I used this code
AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_RINGTONE | AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With