Android: Mediaplayer Setvolume Function

Android: MediaPlayer setVolume function

This function is actualy wonderful. Thanks to it you can create a volume scale with any number of steps!

Let's assume you want 50 steps:

int maxVolume = 50;

Then to set setVolume to any value in this range (0-49) you do this:

float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
yourMediaPlayer.setVolume(log1,log1); //set volume takes two paramater

Nice and easy! And DON'T use AudioManager to set volume! It will cause many side effects such as disabling silent mode, which will make your users mad!

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

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.

How to set android MediaPlayer volume according to ringtone volume?

Instead of adjusting the volume, you should use setAudioStreamType() to set which audio stream you want to play your audio over - this automatically uses the volume of the selected stream. For example, if you want your audio to play at the same volume as a notification would, you could use AudioManager.STREAM_NOTIFICATION:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

Android : How to set MediaPlayer volume programmatically?

Using AudioManager, you can simply control the volume of media players.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

also from MediaPlayer (But I didn't try that)

setVolume(float leftVolume, float rightVolume)

Since: API Level 1

Sets the volume on this player. This API is recommended for balancing
the output of audio streams within an application. Unless you are
writing an application to control user settings, this API should be
used in preference to setStreamVolume(int, int, int) which sets the
volume of ALL streams of a particular type. Note that the passed
volume values are raw scalars. UI controls should be scaled
logarithmically.

Parameters

leftVolume left volume scalar

rightVolume right volume scalar

setVolume Using MediaPlayer API is Not Working

As mentioned in documentation:

"This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. "

https://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float,%20float)

You can however set a stream to your mediaplayer and let is play on
the desired stream like Music / Notification or Alarm which should
suffice the req.

mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

Otherwise if you want to play some sound with certain level you have to use AudioManager API's to set a certain stream & set the volume of the stream and play the audio. This is a common practice.

controlling volume in MediaPlayer

Check to make sure you're not returning true every time onKeyDown is called. If that's the case then you're telling android that you'll handle all onKeyDown events, so android might be ignoring the volume key.



Related Topics



Leave a reply



Submit