I want to disable ALL sound and vibration from the android device. As per the answers to similar questions, I'm currently using the following code to mute all the audio streams, set the ringer mode to silent, and to fake a voice call scenario:
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
//DOESNT DISABLE ALARM CLOCK
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
amanager.setStreamMute(AudioManager.STREAM_RING, true);
amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
amanager.setStreamMute(AudioManager.STREAM_DTMF, true);
amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
amanager.setStreamMute(AudioManager.STREAM_VOICE_CALL, true);
//disables vibrate and sound of ringer
amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
//fakes voice call...changes alarm to single tone+vibrate
amanager.setMode(AudioManager.MODE_IN_CALL);
amanager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
This works for disabling music and incoming calls, but, as noted in the comments, the android's built-in alarm clock app is still able to produce sound and vibration.
Does anyone know how to truly disable ALL sound and vibrations? Or venture a guess as to why the alarm clock app seems to by bypassing the audio streams?
setStreamMute did not work for me to mute the alarm, but using setStreamVolume for STREAM_ALARM setting volume to zero did. It is necessary to restore the alarm volume afterwards.
For example like this:
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
Related
I am using AudioManager and RingtoneManager to mute the background music and start ringing as soon as i receive a call. Which works quite good without issue. But once i take a call. As in below code , i am trying to end ringtone and start the call. But Some how it play muted music streaming long with voice call. How can i differentiate between music streaming and voice call and handle them.
Uri defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(), RingtoneManager.TYPE_RINGTONE, defaultRingtoneUri);
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
r = RingtoneManager.getRingtone(getApplicationContext(), defaultRingtoneUri);
am.setMode(AudioManager.MODE_IN_CALL);
r.stop();
I'm using ExoPlayer in my Android app to play few tutorial videos, and when I pause the video the audio focus gets abandoned and the user is not able to control the media volume via the physical buttons of the device (because the audio focus has been lost the buttons now control the ringtone volume) and I want to keep the audio focus even if the user pauses the video, and I only want it to be lost if the user exits out of the tutorial.
I already tried setting the "exoPlayer.setAudioAttributes" to null which suppose to prevent exoplayer from using its default audio focus settings but when that option is set to null ExoPlayer won't play at all... (I also tried to set to it an actual AudioAttributes like in this post: https://medium.com/google-exoplayer/easy-audio-focus-with-exoplayer-a2dcbbe4640e
but it didn't have any effect)
I also tried setting up AudioManager (the build-in one) and request audio focus but immediately everytime I set it its listener returns "AUDIOFOCUS_LOSS" (I tried setting it before setting ExoPlayer and after, and also I tried setting it again everytime the state of ExoPlayer changes, but every time it returned "AUDIOFOCUS_LOSS" right away.
Here's my code:
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build();
exoPlayer.setAudioAttributes(audioAttributes, true);
You have to use setAudioAttributes for focusing the Audio. Here is the code that works for my project:
val audioAttributes: AudioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.CONTENT_TYPE_MOVIE)
.build()
simpleExoPlayer.setAudioAttributes(audioAttributes, true)
If you want to know in detail you can read this article.
The problem was that I was not setting the stream type for the activity and that's why the hardware buttons weren't controling the music volume when the video was paused (Credit goes to a reddit user for posting this solution over there)
activity.setVolumeControlStream(AudioManager.{INSERT YOUR STREAM TYPE})
Android docs:
https://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)
I have a ContentObserver registered to android.provider.Settings.System that should observe changes of any audio volume. It is notified as expected when clicking the hardware volume buttons but is not notified when I change the audio volume via AudioManager.setStreamVolume or AudioManager.adjustStreamVolume.
Here's how my ContentObserver looks like:
// this is a Service
this.getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI,
true, new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
Log.d("ContentObserver", "got notified");
}
});
And here my call to AudioManager.adjustStreamVolume:
// this.context is the activities context
this.context.getSystemService(Context.AUDIO_SERVICE).adjustStreamVolume(
AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER,
AudioManager.FLAG_SHOW_UI);
I have read this and that post and the AudioManager and Settings.System documentation and can not find a reason why the Observer is notified when changing the volume with the volume buttons but not when changing it with the AudioManager.
Thanks for the help!
The AudioManager does broadcast an intent
android.media.VOLUME_CHANGED_ACTION`
But this is not part of the official documentation. So this might change in future releases. But you could use this atleast for gingerbread devices.
you can find more about the extras in the intent from here
Is there any way within (or non-standard way) to disable email and sms alerts / notifications when an Android application is running? I found a way to mute the phone ringer, but I want to stop the audio / vibrate alerts from occuring. I've seen some bed time clock apps that mute notifications and they work fine.
Any thoughts to how to code this?
According to the API docs:
AudioManager aM = getSystemService(Context.AUDIO_SERVICE);
aM.setRingerMode(AudioManager.RINGER_MODE_SILENT);
aM.setVibrateSetting (AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF)
public void setRingerMode (int ringerMode)
Since: API Level 1
Sets the ringer mode.
Silent mode will mute the volume and will not vibrate. Vibrate mode will mute the volume and vibrate. Normal mode will be audible and may vibrate according to user settings.
Parameters
ringerMode The ringer mode, one of RINGER_MODE_NORMAL, RINGER_MODE_SILENT, or RINGER_MODE_VIBRATE.
public void setVibrateSetting (int vibrateType, int vibrateSetting)
Since: API Level 1
Sets the setting for when the vibrate type should vibrate.
This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.
Parameters
vibrateType The type of vibrate. One of VIBRATE_TYPE_NOTIFICATION or VIBRATE_TYPE_RINGER.
vibrateSetting The vibrate setting, one of VIBRATE_SETTING_ON, VIBRATE_SETTING_OFF, or VIBRATE_SETTING_ONLY_SILENT.
I have an app that will speak text messages to the user. I want to make it so that when the user hits a button called "Headphones on" the app will only speak to it when headphones are detected. Is there a command that will allow me to detect if headphones are plugged in or not?
There is a broadcast made when the headphones are plugged in: http://developer.android.com/reference/android/content/Intent.html#ACTION_HEADSET_PLUG
You need to register a BroadcastReceiver to receive this and perform your required actions.
It seems in this case you just want to check if headphone are connected before to start the audio playout, so you should use audioManager.isWiredHeadsetOn() like this:
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
// Play audio...
}