audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) returns 0 - android

I am making an Speaking caller name application which pause the ringtone before speaking the caller name using TextToSpeech. I am detecting the current Volume using
int musicVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Mostly this returns 0 but sometimes 15 the correct value. The phone is in normal mode and the volume of phone is full. I need this value to correctly speak the caller name but this unpredictable behaviour is making me crazy.
What wrong am i doing? Isn't this the correct way to detect ringtone volume?

It places the example below that it functions, volume is of 0 to the 15.
public void pegarVolume(){
AudioManager AudioManager = (AudioManager) getSystemService (Context.AUDIO_SERVICE);
int musicVolume = AudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
}

Related

How to Change Sound Programmatically on Android

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

Android - Setting in-call volume has no effect

My app launches a Skype call. Before the call is made I want to ensure the in-call volume is set to 75% of the maximum. This is what I've tried ...
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int curVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
int newVol = <do some calculations>
audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, newVol, 0);
<launch Skype call>
However, I'm finding that:
getStreamVolume always returns 5, irrespective of that the volume is actually set to on the device
setStreamVolume has no effect, no matter what value for newVol I use
Anyone know what I might be doing wrong? Also, anyone any idea of what the 'flags' argument for setStreamVolume is used for? It's not described in the API docs.
Thanks
Damian.

volume control with in application in android?

I am preparing an app.My app have one options button with sound on and sound off.For that i used following code,
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Unfortunately, the volume setting for that application would still reflect on the system volume setting for the stream type the application is using. What I would like to know is how to set the volume for that application only without affect the system setting.
Is it possible in android have full control over the volume of the app? Is there any method that could do that? Must I do this with code? please help me
You could use AudioTrack, a lower level sound API. It has an setStereoVolume() which operates independent of the system volume.

Changing volume for one alarm sound only on Android

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);

Override silent mode and/or media volume

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.

Categories

Resources