I have audios which only play when certain conditions are met. I want to increase the audio volume only when it plays, and once it finishes the volume should be set to default system volume. Currently I am setting volume to max, which works ok when audio is playing but keeps the system volume to that level even after audios are finished. Which makes a very bad situation for user when say a call comes it makes the device very noisy.
I am confused to achieve this. I have also read that using audioManager to set volume is not a good practice and it has side effects.
Here is something which I was trying to control the volume:
public void resume() {
AudioManager audioManager = (AudioManager) MobieFitSdkApplication.singleton().getSystemService(Context.AUDIO_SERVICE);
if (player != null) {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
player.setVolume(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
}
}
So how can I only increase volume when audios are playing?
-Do one thing first of all get the current volume of System:
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
int volume_level= am.getStreamVolume(AudioManager.STREAM_MUSIC);
after your audio complete or when you release media player at that time set the default volume which you get before the audio play.
am.setStreamVolume(AudioManager.STREAM_MUSIC,volume_level,0);
I have achieved what I have asked in my question by taking some help from the above answers.
First I am getting the original system volume and once the audio starts I am setting the system volume to 70%.
int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxDeviceVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxDeviceVolume*percent);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
Once the audio ends I am resetting the device volume to the original.
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 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'm using MediaPlayer to streaming audio. Does anyone know how to detect if an audio is too loud? And how to balance it?
You can use AudioManager for that purpose, and get the volume index
AudioManager am = (AudioManager)GetSystemService(AudioService);
int currentVolume = am.GetStreamVolume(Stream.Music);
And check if it has greater volume index than your limit Volume
if(currentVolume > limitVolume){
// Some action
}
i want to play any ring tone/song music irrespective of whether it is in silent mode or vibration mode or any other mode.
the code i am using to play a ring tone is
Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Ringtone ringtoneSound;
ringtoneSound=RingtoneManager.getRingtone(context, ringtoneUri);
if (ringtoneSound != null) {
ringtoneSound.play();
}
but ring tone plays only when mobile volume is high. is there any way to increase the volume of android device ? so that i can increase the volume of my android device and then run the above code.
This should work to set the volume if the phone is a profile authorizing sound.
public void setAlarmVolume(int iVol) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am!=null) {
int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_ALARM);
int volume = (maxVol*iVol)/100;
am.setStreamVolume(AudioManager.STREAM_ALARM, volume, 0);
}
}
iVol is the volume in percent (0 to 100).
Keep in mind that if the device is set on vibration only or on silence, you still won't get sound, and quite rightfully so. You can't bypass the user's wish not to have sound.
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);
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. :)