how to turn speaker on/off programmatically in android 4.0 - android

I play a file through media player and I want to give options like speaker on/off, play though headset, bluetooth ,etc.
I tried the below code which works well for android 2.2 but I want something that can also work for 2.2 and 4.0 both.
Can you help me to programmatically turn the speaker on/off and playing via headphones?
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(isOn){
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setMode(AudioManager.MODE_NORMAL);
}else{
//Seems that this back and forth somehow resets the audio channel
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(isOn);
P.S: I have given this permission in manifest:
android.permission.MODIFY_AUDIO_SETTINGS

Something like this might work on some devices (I've only tested in on an XPeria P):
final static int FOR_MEDIA = 1;
final static int FORCE_NONE = 0;
final static int FORCE_SPEAKER = 1;
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
setForceUse.invoke(null, FOR_MEDIA, FORCE_SPEAKER);
// To get back to the default behaviour, use the combination FOR_MEDIA,FORCE_NONE.
The combination FOR_MEDIA, FORCE_SPEAKER is typically only used internally to route the FM-radio audio to the loudspeaker (since the FM-radio requires you to have a wired headset / headphone plugged in to act as an antenna). Devices that don't have FM-radio functionality (or uses an alternative implementation) might ignore this combination of parameters, so this method would not work on such a device.

AudioManager mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Button mVolumeButton = (Button)findViewById(R.id.btn_Volume);
mVolumeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mAudioMgr.isWiredHeadsetOn()){
mAudioMgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioMgr.setWiredHeadsetOn(false);
mAudioMgr.setSpeakerphoneOn(true);
mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
Toast.makeText(getApplicationContext(), "SpeakerPhone On", Toast.LENGTH_LONG).show();
}else{
mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioMgr.setSpeakerphoneOn(false);
mAudioMgr.setWiredHeadsetOn(true);
Toast.makeText(getApplicationContext(), "Wired Headset On", Toast.LENGTH_LONG).show();
}
}
});

You can acquire either a rear speaker or a front earpiece at time.
If no accessory connected;
Use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_NORMAL); & audioManager.setSpeakerphoneOn(true);
If accessory connected; Use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(false); to use front speaker/earpiece. But this would play audio in earpiece not on speaker. To use rear speaker, use audioManager.setMode(AudioManager.MODE_IN_CALL); & audioManager.setSpeakerphoneOn(true);
Note: Make sure audioManager.setWiredHeadsetOn(boolean on) and audioManager.setBluetoothScoOn(boolean on) set to false to route audio via earpiece . And set either to true to route audio accordingly.

try follow code snippet:
//for speakerphone on
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(true);
//for headphone on
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.setSpeakerphoneOn(false);
BTW,I tested in Android 7.0(Redmi 4x) and it worked fine.

Problem solved. For all of you who are still searching for the answer. Well this is not a bug , but just a tricky thing . You need to use the PhoneStateListener
Using this guide : http://danielthat.blogspot.co.il/2013/06/android-make-phone-call-with-speaker-on.html

if u just want to open your speakerphone on u just write this line in oncreate() of your activity.
static AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);

Try This.
AudioManager audioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
if (isOn) {
isOn = false;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setMode(AudioManager.MODE_NORMAL);
} else {
isOn = true;
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setMode(AudioManager.MODE_IN_CALL);
}
audioManager.setSpeakerphoneOn(isOn);

Try
AudioManager.speakerphone(true);
Look at this.

Related

AudioManger FLAG_SHOW_UI not showing the system volume menu in Samsung devices

AudioManager ad = (AudioManager) getSystemService(AUDIO_SERVICE);
if (ad != null) {
ad.adjustVolume(AudioManager.STREAM_MUSIC,AudioManager.FLAG_SHOW_UI);
}
This code doesn't showing volume menu in Samsung devices.
Tried with Galaxy S10,A71
For your case STREAM_MUSIC you need to use like below code -
AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
Hope this will help you.

How to mic mute when recording video?

How to mute mic in android my code is working but in some device mic is not muted please give solution my code is:
private void setMicMuted(boolean state){
AudioManager myAudioManager = (AudioManager)con.getSystemService(Context.AUDIO_SERVICE);
// get the working mode and keep it
int workingAudioMode = myAudioManager.getMode();
myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
// change mic state only if needed
if (myAudioManager.isMicrophoneMute() != state) {
myAudioManager.setMicrophoneMute(state);
}
// set back the original working mode
myAudioManager.setMode(workingAudioMode); }
If you are using MediaRecorder just use it without calling setAudio on it, this way it will only record video without audio.

Android system audio not working correctly after using setSpeakerPhoneOn of AudioManager

I'm trying to make a application that switches the audio play between the speaker and earpiece.
I'm using The following code:
public boolean setAudioMode(String mode) {
AudioManager audioManager =
(AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
if (mode.equals("earpiece")) {
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(false);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
return true;
} else if (mode.equals("speaker")) {
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(true);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
return true;
}
return false;
}
This code is working and I can change the audio output correctly.
But after I close my application, my phone does not play any sound through my headphones and the volume control sticks to in call volume. My phone only go back to normal after a full restart.
What can I do to restore the phone audio? Or is there a better way to switch the audio output that does not make this problem?

Automatically silence the sound volume of Android Phone programmatically?

I'm developing an application that can turn off the sound of Android Phone automatically. How can I detect the volume of the sound and turn it off programmatically?
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,
"It's time to work");
///so, i want to add the silet function here..help me, please?
}
Thanks in advance.
Have a look at the AudioManager, especially the getStreamVolume and setStreamVolume methods
EDIT
You can also use the method Nikola Despotoski provided with setRingerMode
A Service is a child of a Context so you can call directly getSystemService
See the updated code below (untested):
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,"It's time to work");
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
Register for AUDIO_SERVICE and then use the AudioManager to control the volume up/down or set profiles.
Or if you want to listen for changes in the Audio focus then make your Activity implements AudioManager.OnAudioFocusChangeListener. Override unimplemented method. Create switch that will take care of types of changes.
Like:
#Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_GAIN:
//do something
break;
case AudioManager.AUDIOFOCUS_LOSS:
break;
}
Or if you want to listen for changes on the audio output, like unplugging the headphones (switching to phone speaker) use ACTION_AUDIO_BECOMING_NOISY sticky broadcast
http://developer.android.com/reference/android/media/AudioManager.html#ACTION_AUDIO_BECOMING_NOISY
http://developer.android.com/reference/android/media/AudioManager.html#RINGER_MODE_SILENT
See here
Edit: This is the solution. There was no need to handle AudioFocus but just set different ringer profile or adjusting volume
if (hour == myTime.getHour() && minute == myTime.getMinute()) {
if (Settings.getSetMyTime(this))
showNotificationAlarm(R.drawable.icon,
"It's time to work");
AudioManager audiomanager =(AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT); //or adjust volume here instead setting silent profile for the ringer
}

Android : setVolume and setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

I am playing an audio file with an internal speaker using this code
audioManager = (AudioManager)Context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
How can I set the volume?
Use adjustStreamVolume() on AudioManager.
Though, preferably, you let the user set the volume the normal way, via the volume control buttons. You can indicate what stream that is to control in your activity via setVolumeControlStream().
am2 is an instance of AudioManager system service.
am2 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// makes the media volume adjustment
public static int setVolume(int inputVol, Context sender) {
int outVol;
if (inputVol < 0)
inputVol = 0;
if (inputVol > am2.getStreamMaxVolume(AudioManager.STREAM_MUSIC))
inputVol = am2.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
am2.setStreamVolume(AudioManager.STREAM_MUSIC, inputVol,
AudioManager.FLAG_SHOW_UI);
outVol = am2.getStreamVolume(AudioManager.STREAM_MUSIC);
return outVol;
}

Categories

Resources