6

I am trying to play sound via media player on STREAM_SYSTEM

using

mediaPlayer.setVolume(1.0f , 1.0f);

or

mediaPlayer.setVolume(0.90f , 0.90f);

But it is playing on vol as per Hardware volume controls in settings.

Basically this line has no effect.

I have added

permissions "Modify..." as well in Manifest

But i saw links in stackoverflow where this API works.

I don't wanna go to API

AudioManager.setStreamVol()....

as it sets stream vol for all apps

What is problem that this setVol API is not working??

Can anybody give a link to working sample??

Rohit Singhal
  • 381
  • 1
  • 4
  • 22

4 Answers4

8

I faced the same issue. I thought that if I use 0.5f as parameter, I could tell the difference. I tried 0.2f, still no difference. So I thought there was a problem indeed. I finally tried 0.02f just in case, and finally I had a lower volume.

Sample:

replace your

mediaPlayer.setVolume(0.90f , 0.90f);

with

mediaPlayer.setVolume(0.09f , 0.09f);
Max
  • 13,537
  • 6
  • 51
  • 68
Georges
  • 277
  • 3
  • 12
  • you can also divide the current stream volume by `15f` like: `float volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) / 15f;` and then `mediaPlayer.setVolume(volume, volume);` – Pierre Sep 13 '18 at 05:29
1

You must put the volume value between 0.0f and 1.0f. So 1.0f is 100% of the volume. Because of that your line has not effect.

Lennon Spirlandelli
  • 2,971
  • 5
  • 23
  • 50
0
AudioManager audioManager;
int actualVolume;
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actualVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, actualVolume + 1, AudioManager.FLAG_SHOW_UI);

The above code is taken from my working project, where it is used to increase the volume of the system (I used it for TV). You can use actualVolume - 1 to lower down the volume.

Pranit Bankar
  • 443
  • 1
  • 7
  • 21
Nitin Misra
  • 4,392
  • 3
  • 31
  • 52
-1

Using AudioManager you can control the volume of media player.

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

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

public void  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

  • For the same reason as mentioned in Doc, I am using setVolume (float leftVolume, float rightVolume) But it is playing at volume as that of stream on which i am playing – Rohit Singhal Jan 22 '15 at 11:39
  • At least mentioned your source: https://stackoverflow.com/questions/8238231/android-how-to-set-mediaplayer-volume-programmatically – Kambiz Feb 13 '18 at 22:09