I am working on a life saving medical app and if the user is in a life threatening situation, they need to hear the alert.
When I have a notification to the status bar or have a dialog appear for a critical message to the user, I need to get their attention. If the media volume or ringer volume is low or off, I want to override it for my alert only. I would prefer not to change the settings for the phone, just for my one sound that I want to play.
When I try:
AudioManager audioManager = (AudioManager)getSystemService(this.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
This correctly sets the volume for my stream but has the side effect of changing the stream volume for everyone else.
Is there a way of setting the volume for one song only?
It could be set back after the song is done.
AudioManager audioManager = (AudioManager)getSystemService(this.AUDIO_SERVICE);
int current_volume=audioManager.getStreamVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_RING,
audioManager.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
// Play here
audioManager.setStreamVolum(AudioManager.STREAM_RING,current_volume,0);
Related
I have a media player which plays song files. However, no matter how I try to initialize its volume, the only way to change it is manually with the volume buttons. I've tried
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume, 0); // Sets volume to max
and even
mMediaPlayer.setVolume(1, 1);
but none work. I've used this code in the past without problem. I've tried my app on both 5.1.1 and 7.1.1 and no luck. It doesn't matter whether the phone's volume starts in a muted state or not. I checked and maxVolume is non-zero (I've tried just hardcoding numbers too). How can I set the initial volume programmatically? The media player starts playing automatically. (I've tried calling this within the media player's onPrepared listener too in case it made a difference. It doesn't.) I also checked whether the phone volume is "fixed". It's not.
How can I get my player to start playing at max volume (no matter what the phone was set for)?
I found the problem. I had the stream wrong. Instead of STREAM_ALARM it should have been STREAM_MUSIC. The list of streams can be found here:
https://developer.android.com/reference/android/media/AudioManager.html
I have tried finding an answer to this, but my searches kept returning irrelevant results...
So the problem is - I am writing a game that plays sound effects (no music just yet), and I set the stream type to Music. However, when I put my phone in silent mode (no vibration either, if that matters) the app still plays sounds. I can turn off the sound in the app using the volume keys, but what I expected (and what my future users would probably expect too) was that the app won't make a sound when the phone is in silent mode.
At this point, I am not sure if this should be automatically managed by the OS, or if I am expected to do something about it. I can surely detect the silent mode, set the volume to 0 in e.g onResume, but that will probably overwrite the setting the users set up by pressing the volume buttons - so when they unmute the phone, the app either has to set the volume programmatically to a predefined value, or ideally, to a saved one from sharedPrefs... which sounds relatively cumbersome. Is there a nicer solution?
This is how I initialize & then use sounds:
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new SparseIntArray();
soundPoolMap.put(CLICK_1, soundPool.load(activity, R.raw.click1, 1));
soundPoolMap.put(CLICK_2, soundPool.load(activity, R.raw.click2, 2));
...
float volume = 1.0f;
soundPool.play(soundPoolMap.get(soundID), volume, volume, 1, 0, 1f);
What I understand from your question is you want to mute your app when device is on silent mode.
Try to check the the RingerMode every time before your app play sound/music and play sound only when the ringer RingerMode is normal.
Here is the sample code.
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (audio.getRingerMode())
{
case AudioManager.RINGER_MODE_NORMAL:
// Device is on Normal mode.
// you can play music.
break;
case AudioManager.RINGER_MODE_SILENT:
// Device is on Silent mode.
// you should not play sound now.
break;
case AudioManager.RINGER_MODE_VIBRATE:
// Device is on Vibrate/Meeting mode.
// you should not play sound but you can make vibrate device (if you want).
break;
}
I want to connect Android OS default tick sound (for example, the sound you hear when you long click Home button and select previous app to start) with my button click. I know how to play sounds via MediaPlayer, but I do not know where to search for this default tick sound. It had to be in some default resources, but I could not find it.
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
mp.setLooping(false);
mp.start();
Anyone can help?
PS. this sound will be activated inside of onClick method.
PPS. I know I can user /raw dir, but I do not think there's a need for it. Not to say, it's cooler to play this tick sound prepared for user's phone.
You can play the default Android 'tick' sound using the view.playSoundEffect() method on any View - surprisingly enough, all views can play a selection of 'system' sounds:
view.playSoundEffect(SoundEffectConstants.CLICK);
and don't forget to add this to your view :
android:soundEffectsEnabled="true"
This is probably the simplest answer to your problem :)
If you want control over the volume of the sound, use the AudioManager system service. Plus this can be used from a Service if you don't have a view handy.
// get the AudioManager once onCreate or similar
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
...
// Play a System Sound
audioManager.playSoundEffect(SoundEffectConstants.CLICK);
// OR at 50% Volume
audioManager.playSoundEffect(SoundEffectConstants.CLICK, 0.5F);
If you are on Google GLASS you can use the com.google.android.glass.media.Sounds constants
audioManager.playSoundEffect(Sounds.TAP);
The following are included in Sounds
DISALLOWED, ERROR, SUCCESS, TAP, SELECTED, DISMISSED
Roberto Tyley's answer is correct.
You can play a sound from every view just by calling it this way:
Button01.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
v.playSoundEffect(SoundEffectConstants.CLICK);
}
});
Just note that the sound will not play if touch sounds are off by default. This is set in the general device sound preferences (Settings-->Sound-->Audible or on newer OS: Options > Sound > Touch)
Also, if this setting is set, most click events will trigger the click sound anyway!
You can use tick, beep or any kind of inbuilt sound by Tone Generator
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
NOTE: This is a pretty old answer. Check Roberto Tyley's answer below.
I think the sound that you are looking for is and is in - /system/media/audio/ui/KeypressStandard.ogg
I think you can give that path to the SetDataSource API of the mediaplayer. But I am not really sure if it will have the same name in all android phones.
There might be a better way to query for default click sound..
I'm at a lost. I want to be able to adjust the speak volume. Whatever I do, I can't increase its volume. How do I make it as loud as that found in the Android settings (as below)?
System Settings -> Voice input and output -> Text-to-Speech settings -> Listen to an example
My code at this moment is:
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
int loudmax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,loudmax, AudioManager.FLAG_PLAY_SOUND);
mTts.speak(name,TextToSpeech.QUEUE_FLUSH, null);
Try using AudioManager.STREAM_MUSIC when calling the setStreamVolume(...) method. The example speech is affected by the media volume if I adjust the volume of music playback on my phone so I guess STREAM_MUSIC is what you need.
EDIT: This code works perfectly for me...
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int amStreamMusicMaxVol = am.getStreamMaxVolume(am.STREAM_MUSIC);
am.setStreamVolume(am.STREAM_MUSIC, amStreamMusicMaxVol, 0);
tts.speak("Hello", TextToSpeech.QUEUE_FLUSH, null);
The max volume for STREAM_MUSIC on my phone is 15 and I've even tested this by replacing amStreamMusicMaxVol in my call to am.setStreamVolume(...) above with the values 3, 6, 9, 12, 15 and the volume of the speech is correctly set.
In your code you are changing the volume of notifications. Is the volume of TTS played at the same volume level as notifications? I suspect it isn't and it probably played at either STREAM_SYSTEM or STREAM_MUSIC Try changing the stream type to one of these:
STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSIC or STREAM_ALARM
I want to override the silent mode and/or media volume to make the phone broadcast a loud noise. I know the alarm clock can override silent mode. How do you do this through your app?
Are you asking whether the Alarm Clock app can override silent mode or if code can override silent mode?
The code answer is yes, you can change the silent mode setting via code like this:
AudioManager audio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int max = audio.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio.setStreamVolume(AudioManager.STREAM_RING, max, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
I got another way, as the Tony's answer was worked but what happen it will change the profile suppose you put your device into the silent mode and app required to play any sound like alarm tone then it will change your profile silent to ringer mode and then again you need to put into silent mode. Am I right?
So the another solution was you can play the ringtone here snippet given below
int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
if(volume==0)
volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
ringtone = RingtoneManager.getRingtone(getApplicationContext(), Uri.parse(ringTonePath));
if(ringtone!=null){
ringtone.setStreamType(AudioManager.STREAM_ALARM);
ringtone.play();
isRinging = true;
}
isRinging set as flag if you want to programatically stop the playing or you can check the by isPlaying() of ringtone to stop playing
going through this I didn't change the current profile and no more code for that
Prey is an open source phone tracker app which does this as one if its features - if you don't get a better answer could have a look at the source to see how they did it.