Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing sound output through speaker in Android

Tags:

java

android

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.

like image 909
Icemanind Avatar asked May 26 '10 22:05

Icemanind


3 Answers

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.

like image 51
Roman Nurik Avatar answered Oct 17 '22 22:10

Roman Nurik


Sound output through speaker

AudioManager m_amAudioManager = 
        AudioManager)getSystemService(Context.AUDIO_SERVICE);  

m_amAudioManager.setMode(AudioManager.MODE_NORMAL); 
m_amAudioManager.setSpeakerphoneOn(true); 
like image 43
Ram Avatar answered Oct 17 '22 22:10

Ram


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);
like image 44
Héctor Quintero Avatar answered Oct 17 '22 22:10

Héctor Quintero