How do I get the system volume programatically - android

I want to get the system volume level to use programatically, the one you set with the hardware keys when being outside any application.
I tried with below method but it doesnt do anything
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.notif);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int volume_level = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
mediaPlayer.setAudioStreamType(volume_level);
mediaPlayer.start();
What do I need to change?

You have to change this line
mediaPlayer.setAudioStreamType(volume_level);
to this:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
setAudioStreamType()
Used to identify the type of audio stream
AudioManager.STREAM_ALARM is for alarm
AudioManager.STREAM_DTMF is for touch sounds
AudioManager.STREAM_MUSIC is for music
AudioManager.STREAM_NOTIFICATION is for notification
AudioManager.STREAM_RING is for phone ring
AudioManager.STREAM_SYSTEM is for system sounds
AudioManager.STREAM_VOICE_CALL is for phone calls

Please reffer offical doc here to set or get the system volume of android device.
To set volume level to 20 you can use following code:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

Related

Playing voicemail via MediaPlayer turns alarm volume silent

I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?
Code for the file I am using to control the voicemail playback.
http://hastebin.com/ehijobuwoc.coffee
You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.
http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.
I needed to setMode back to what it was when I started, in onStop():
// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);
and in init:
private void initMediaPlayer() {
// Make the media play through earpiece instead of through speaker(default)
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
prevMode = am.getMode();
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(false);
Thanks #DreadfulWeather for leading me to this answer, with some helpful questions.

MediaPlayer.setVolume() function doesn't seem to work

I need to set media volume to MAX in my app to play a buzzer.
I am trying to do it by using media.setVolume() function but it doesn't seem to work.
I have already tried
mediaPlayer.setVolume(1.0f, 1.0f);
I have also tried
int MAX_VOLUME = 1000;
final float volume = (float) (1 - (Math.log(MAX_VOLUME - 999) / Math.log(MAX_VOLUME)));
mediaPlayer.setVolume(volume, volume);
None of the above worked for me.
Somebody pls help me on how to set media volume to full using MediaPlayer.setVolume(float, float) function.
MediaPlayer.setVolume(float, float) sets the volume of the given MediaPlayer instance. This volume is 1.0f (max) by default. It doesn't change the global media volume which is what I wanted to accomplish originally.
I found a solution that simply sets the global media volume.
Useful Remark: I found many answers on stackoverflow.com for setting max volume level or changing volume, most of them used alarm stream (STREAM_ALARM) to do so. I think using alarm stream would not be a good option if you are playing audio casually.
The global volume of a stream type (music in this case) can be changed using the following code.
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Now, play your media object as a Music Stream :
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Make sure that you request MODIFY_AUDIO_SETTINGS permission in your application's manifest.
<uses-permission
android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Note: This only sets Media (Music) Volume to the max. To set other Volumes like Ringer, use STREAM_RING.
Thanks, #MrTristan for your advice, it was really helpful.
make sure you've got MODIFY_AUDIO_SETTINGS set as a permission you request in your app if that's the type of volume you're looking to set.
Note that the passed volume values are raw scalars in range 0.0 to 1.0
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
Why don't you try just using mp.setVolume(1.0, 1.0).
setVolume() doesn't work properly, so you should set stream volume by AudioManager.
This one has a bad effect too and it is that you change the entire stream volume of the user device!
And user doesn't like this, so you should change it back to the default value.
But how?!
Define your AudioManager:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
Store current volume and set stream value to its Maximum value:
int currentVolume = Objects.requireNonNull(am).getStreamVolume(AudioManager.STREAM_NOTIFICATION);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
Define AudioAttributes.Builder and set the stream type for it:
AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
audioAttributes.setLegacyStreamType(AudioManager.STREAM_NOTIFICATION);
Set audio attributes for your MediaPlayer before calling prepare() or prepareAsync():
mediaPlayer.setAudioAttributes(audioAttributes.build());
Finally set a on completion listener for mediaPlayer and change the volume to its default:
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, currentVolume, 0);
}
});
Finish! You could handle the problem. :)

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.

Audio control in Android app

I am working in an android app and I need to make an optionMenu with an option to disable/enable the sounds in the app.
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?
Thanks
You can find useful information here. According to that answer, it's not possible. Just don't play the sounds.
By using AudioManager you can get current volume level and max volume level. At the time of button click/ option select you can control the volume level also. Here is the code.I hope this will help you to set volume.
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
To Set Volume
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,vol_level, 0);

How to set volume for text-to-speech "speak" method?

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

Categories

Resources