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
}
Related
I am a bit confused because of this question AUDIOFOCUS_GAIN doesn't called after AUDIOFOCUS_LOSS_TRANSIENT after a phone call
I don't believe in "Busy Waiting" solution. I think that AudioManager changes the state to AUDIOFOCUS_GAIN
And this question supports my viewpoint AUDIOFOCUS_LOSS called after a phone call in android
but when I tried, it never occurs to have the case of AUDIOFOCUS_GAIN
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
the request sent by
am.requestAudioFocus(af, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
and
OnAudioFocusChangeListener af = this;
and
public void onAudioFocusChange(int i) {
Log.d("AudioChanged", i + "");
if (i == AudioManager.AUDIOFOCUS_LOSS) {
am.abandonAudioFocus(this);
sendBroadcast(new Intent(Constants.ACTION.PLAY_ACTION));
} else if (i == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) {
am.abandonAudioFocus(this);
sendBroadcast(new Intent(Constants.ACTION.PLAY_ACTION));
Log.d("loss transient", "AUDIOFOCUS_LOSS_TRANSIENT");
} else if (i == AudioManager.AUDIOFOCUS_GAIN) {
Log.d("gain ", "AUDIOFOCUS_GAIN");
startPlaying();
}
}
You shouldn't abandonAudioFocus() when AudioManager.AUDIOFOCUS_LOSS_TRANSIENT or when AudioManager.AUDIOFOCUS_LOSS, it happens automatically (the audio focus is passed to the new application that request audio focus, you just get the callback and you shoudn't abandon the request for audio focus becuase in case of a phone call you want to play again your music, what you should do is in case of AudioManager.AUDIOFOCUS_LOSS, stop your MediaPlayer and that's all and in case of AudioManager.AUDIOFOCUS_LOSS_TRANSIENT pause your MediaPlayer and when the phone call ends you automatically will get a call to AudioManager.AUDIOFOCUS_GAIN and then you should play again your MediaPlayer.
When I play 'Soundcloud' or 'Saavn' music playing app and I start my own app, the music in Saavn or Soundcloud is still playing in background. Need a way to stop it using code. How to do it ?
I tried this -
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
String SERVICECMD = "com.android.music.musicservicecommand";
String CMDNAME = "command";
String CMDSTOP = "stop";
if(mAudioManager.isMusicActive()) {
Intent i = new Intent(SERVICECMD);
i.putExtra(CMDNAME , CMDSTOP );
HomeActivity.this.sendBroadcast(i);
}
but it is only useful in stopping the music if Android's default music player is playing in background and not 3rd party apps like Saavn and Soundcloud.
One possible solution I found on this is using the AudioManager.requestAudioFocus(...) function
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Code example:
int result = am.requestAudioFocus(focusChangeListener, //Request audio focus for playback
AudioManager.STREAM_MUSIC, //Use the music stream.
AudioManager.AUDIOFOCUS_GAIN); //Request permanent focus.
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// other app had stopped playing song now , so u can do u stuff now .
}
Audio focus is assigned in turn to each application that requests it. This means that if another application requests audio focus, your's will lose it.
The app is notified when there is a change on the AudioFocus, through the onAudioFocusChange event listener. This listener is the first parameter in the requestAudioFocus function.
This listener should look something like:
private OnAudioFocusChangeListener focusChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK):
//Lower the volume while ducking (not sure what ducking means really)
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
//TODO: pause audio
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
//TODO: pause audio
break;
case (AudioManager.AUDIOFOCUS_GAIN):
//TODO: Return the volume to normal and resume if paused.
break;
default: break; //empty on default
}
}
};
I think it is not possible to stop a service of another app as you may not know the processid, You can stop the service of your app only if you know the Pid of the app using
android.os.Process.killProcess(processIdKillService)
Background
I'm developing an app that monitors the proximity sensor. However, I don't want to monitor the sensor while the user is listening to something through the phone ear speaker. This is because the user will probably have his/her head against the handset to listen, which in turn triggers the proximity sensor, and I'm not trying to detect head proximity.
Question
How can I detect when sound is and isn't playing through the handset ear speaker? Callbacks are preferable, of course, but I'm willing to poll if it's the only way.
You can approximate this by regularly polling and making the following checks:
Check if the audio system is not in normal mode.
Check if the audio is being routed to the earpiece.
I tested this in Android 4.3, and it seemed to work fine with the system phone app, Viber and Skype. However, it doesn't seem to detect music or non-telephony sounds played through the earpiece. I don't think this is much of a problem, because the earpiece generally seems to only be used for telephony anyway.
Example
public class EarpieceSpeakerState {
private AudioManager audioManager;
public EarpieceSpeakerState(AudioManager audioManager) {
this.audioManager = audioManager;
}
public boolean usingEarpieceSpeaker() {
return playingSound()
&& routingToEarpiece();
}
private boolean playingSound() {
return audioManager.getMode() != AudioManager.MODE_NORMAL;
}
private boolean routingToEarpiece() {
return !(
audioManager.isSpeakerphoneOn()
|| audioManager.isBluetoothScoOn()
|| audioManager.isBluetoothA2dpOn()
|| audioManager.isWiredHeadsetOn()
);
}
}
I don't know if it is the right solution but i think you should at least give it a try, check out this link.
Teorically you will not receive focus if somebody else is using the speaker of your phone.
base solution, you can extend it link
private fun initOnAudioFocusChangeListener() {
val afChangeListener: AudioManager.OnAudioFocusChangeListener =
AudioManager.OnAudioFocusChangeListener { focusChange ->
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> {
viewModel.isAppLostAudioFocusLiveData.value = false
}
else -> {
viewModel.isAppLostAudioFocusLiveData.value = true
}
}
}
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
audioManager.requestAudioFocus(
AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(afChangeListener).build()
)
} else {
audioManager.requestAudioFocus(
afChangeListener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
}
}
Im using this code to get AudioFocus and it works ok with
Android Music app ( the one preinstalled )
int result = audioManager.requestAudioFocus(meService, AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
However when i release it with this code
audioManager.abandonAudioFocus(meService);
The Android Music app ( the one preinstalled ) does not continue playing.
if i use the AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK it works
but Android Music app is not lowering the volume enough.
Any ides why Android Music app is not resuming playback?
im using api8 and using the onAudioFocusChange
You should release it in MediaPlay.onCompleteListener(){}
After trying 4 players non of them are ducking. I might be doing something wrong and would like to see that. I answer my own question that we have to live with this.
This way works fine for me.
public class HomeActivity extends Activity implements AudioManager.OnAudioFocusChangeListener {
// Other stuff
#Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Log.i("HomeActivity", "Audio focus granted.");
}else if (focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
Log.i("HomeActivity", "Audio focus failed.");
}
}
}
Request AudioFocus:
private void requestAudioFocus(){
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
// Request audio focus for playback
am.requestAudioFocus(this,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
}
Abandon AudioFocus:
private void abandonAudioFocus(){
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
am.abandonAudioFocus(this);
}
Hope this would help you.
Does anyone have the same problem?
On Android's Phone app, whenever a number is pressed, the tone is always smooth.
But, when I used very similar code as the Phone app, the tone I get regularly isn't smooth...there are gaps. Is there a way to resolve this?
Some of my theories are that the emulator causes these breaks in sound as there are time lags in processing. The Phone app on the emulator is also more "compiled"/native than my code. Etc. Don't know what is the reason for these tones not being continuous.
Here's the code (literally the same as Phone app):
...
playTone(ToneGenerator.TONE_DTMF_1,150);
...
void playTone(int tone) {
// if local tone playback is disabled, just return.
if (!mDTMFToneEnabled) {
return;
}
// Also do nothing if the phone is in silent mode.
// We need to re-check the ringer mode for *every* playTone()
// call, rather than keeping a local flag that's updated in
// onResume(), since it's possible to toggle silent mode without
// leaving the current activity (via the ENDCALL-longpress menu.)
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int ringerMode = audioManager.getRingerMode();
if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
|| (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
return;
}
synchronized (mToneGeneratorLock) {
if (mToneGenerator == null) {
Log.w("test", "playTone: mToneGenerator == null, tone: " + tone);
return;
}
// Start the new tone (will stop any playing tone)
mToneGenerator.startTone(tone, TONE_LENGTH_MS);