When someone calls, my app will turn off the ringing tone in order not to distract the user. So could you please tell me how to turn off the ringing sound in Android?
EDIT:
It seems I will need to use this one :http://developer.android.com/reference/android/media/Ringtone.html
I think I've found the solution. In case someone else needs it, I copy my solution here:
AudioManager manager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
manager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
There is a manager class called AudioManager which controls the audios and such. And it has a instance method setRingerMode(int mode) which accepts a integer flag.
Once you're done with the silence, you can change the status of the ring tone by passing in this flag RINGER_MODE_NORMAL
Related
I'm wondering, what is the best way to handle the AudioManager in Android, for media.
As you know, AudiManager manage different type of audio, like music or ringtone.
If I have an applicaiton playing sound effects and vibrating, with 3 activity, how do I have to handle this class ?
-Do I have to set the volume on the onCreate method in each activity ?
Then use it
audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);
-Or do I have to make a static reference, and use it each time ?
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
-Or is these wrong, and I didn't understand how it works ?
To tell the truth, I'm having problem handling this im my 3 activities : in the first 2 activities, I have sound effect, and in the 3rd one, only vibration. In the first activity, the sound is in "Ringtone mode", and the other two "Media mode". So the sound volume is different which is bad.
In order to use the AudioManager you have to have an instance of it (in every Activity or Service you want to use it) so:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
then you can use it:
audioManager.setVolumeControlStream(AudioManager.STREAM_MUSIC);
This doesn't need to be called inside the OnCreate() method but need to be called before you play the sound.
I my game application am using musics. I need mute and unmute button to put mob i silent mode.
Music runs in every activity like playing game and checking score.But mute button is added in menu activity.
I googled didn't get any exact result.
Use audio manager and set volume
AudioManager audioManager = (AudioManager) getSystemService(ctx.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
I'd choose to set the volume to 0 (mute) or 1 (unmute) with MediaPlayer.setVolume(float volumeLeft, float volumeRight). Also preserves the users volume settings. Trigger by Button is easily achieved with onClickListener().
I have seen a lot of questions about ringer mode. Nothing seems to give me a clue on the problem I am facing. I have set ringer mode to the normal RING mode for a service that gets started in the foreground. Unfortunately that doesnt seem to change the phone's ringer mode if it was on silent or vibrate in the first place. However, if put the same piece of code behind a button on an activity then it works.
The above described case happens only in Android 4.0.4 (ICS) but not in FR 2.2. Any clue on how to get around this? Here's my code:
AudioManager audioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
I have figured out why it didnt work on my phone. I had an other app called Flip Silent that interfered with the working of my app. After everytime I set the Audio Manager to ringer mode, it's service set it it back to the exisiting mode leading me to think it wasnt working.
it was difficult to debug as the logs didnt lead me in any direction. But a flicker on my home screen lead me to find this out. I uninstalled the flip silent app and everything worked!!
I also face the same problem so, I slove that this way.
int getMediaValue = audioManager.getStreamVolume(audioManager.STREAM_RING);
int setVolume = getMediaValue - 1;
// This was work for ring volume
audioManager.setStreamVolume(audioManager.STREAM_RING, setVolume, AudioManager.FLAG_SHOW_UI );
So you can try this way :)
I have certain data which i got, when user fill a form in an activity. There is an option Mode where user can select RING or VIBRATE.
So my question is how could i actually implement it in my activity, I see various examples on Telephony manager and phonestatelistener
http://marakana.com/forums/android/examples/62.html.
http://www.tutorialforandroid.com/2009/01/get-phone-state-when-someone-is-calling_22.html
but its hard to implement it in my application,i have data in text form only and dont know how to use this data to switch it from one mode to another. Please tell me in terms of coding example.
phone state listener doesnt determine the ring or vibrate mode - it is for the call status (idle, ring, offhook)
to get the ring/vibrate/silent stau use Audiomanager
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.getRingerMode();// returns AudioManager.RINGER_MODE_VIBRATE or AudioManager.RINGER_MODE_SILENT or AudioManager.RINGER_MODE_NORMAL
// to set ringer mode
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
I'm developing an media player application for Android, for which I need to handle any Alarm notification, and based on that I'll pause my playback. When the Alarm in snoozed or dismissed, I'll then resume the playback.
I googled a lot for the Alarm handling, but what I found was the way to enable Alarm notifications through code, set the intent and then handle it. However, no where could I locate just handling the Alarm notification part. I don't need to set the Alarm on, it could've been set by the user, and I don't need to programmatically. All I need is just handle that notification.
Any ideas on this would be extremely useful?
Thanks,
Asheesh
HI Asheesh Vashishtha,
Correct me on this, but AFAIK whenever any other application even if it is the alarm clock, is activated, your activity will surely go in background. So i guess u can override the OnPause and OnResume functions to put your bit of code. As far as snooze or other things are concerned, they all will result in the Alarm Activity getting destroyed(or paused, don know much about it) and your activity will get resumed. So that wont be a matter of concern for u!
Hope this helps...
AFAIK, there is no way for you to be notified of what the Alarm Clock application does, any more than you get notified about any other third-party alarm clock.
Note that AlarmManager -- what you were probably reading about -- is not the same as the Alarm Clock application.
Sorry!
I ran into a similar situation while developing a media player. My solution was to use the AudioManager's OnAudioFocusChangeListener.
You implement the listener in the class like so
public class VideoPlayerHelper implements AudioManager.OnAudioFocusChangeListener {
Then you override onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
//Just fall through by omitting break
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_LOSS or AUDIOFOCUS_LOSS_TRANSIENT"); //Custom logging class
if (isPlaying()) {
pause();
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
}
break;
case AudioManager.AUDIOFOCUS_GAIN:
LogUtil.log(LogUtil.DEBUG, TAG, "AUDIOFOCUS_GAIN"); //Custom logging class
break;
default:
break;
}
}
The key here is AudioManager.AUDIOFOCUS_LOSS_TRANSIENT. This was the code the listener kept receiving when the alarm clock would go off (on The Note 5). So I simply handled AudioManager.AUDIOFOCUS_LOSS_TRANSIENT the same as AudioManager.AUDIOFOCUS_LOSS by pausing the media player and letting go of the audio focus.
When we setup the media player, I added this line before adding the data source
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure your code for starting the media player also has this line in it (I have it in the start code and onResume code in case the alarm went off while the app was in the background).
mAudioManager.requestAudioFocus(VideoPlayerHelper.this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
That line helps you get back the audio focus when you hit the play button after dismissing the alarm clock.
You should also let go off audio focus when you're finished with the media player. I put this line of code in the onStop and onDetach methods.
mAudioManager.abandonAudioFocus(VideoPlayerHelper.this);
It's not as much setup as you may think and it allows you to adjust your media player whenever unexpected audio is introduced (such as an alarm clock or timer goes off).