23

I know the question can be regarded as "politically incorrect", but I'm designing an app which "by design" must get the attention of people within the maximum possible distance range, otherwise it will not be used... :-)

I'm currently using SoundManager class, and this is the code which plays my ogg clip:

public void playSound(int index) { 
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); 
}

The problem is that the sound volume I get the clip played with appears to be dependent by "Settings/Audio/Voulme" settings the user has set. Instead it appears to be indipendent by the hardware volume buttons setting.

Is there a way for an Android app to play a sound to the maximum physical volume allowed by the device?

MarcoS
  • 17,323
  • 24
  • 96
  • 174

5 Answers5

55

I'd suggest using getStreamMaxVolume and setStreamVolume to do this:

int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);

Then once you're done just set it back to the original volume.

I think I was beaten to the punch, ahh well :)

Some code that actually does this, I'm using the MediaPlayer rather than the soundpool as this gives you a play complete callback which doesn't appear to be present on the soundpool:

final AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
final int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
MediaPlayer mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource("content://media/internal/audio/media/97");
mp.prepare();
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
   @Override
   public void onCompletion(MediaPlayer mp)
   {
      mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, originalVolume, 0);
   }
});

Btw the with call mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, 1.0f); the streamVolume values are actually floats 0 -> 1 that represent a percentage of the maximum value so you'd really just want to put in 1.0f there.

zeetoobiker
  • 2,789
  • 1
  • 18
  • 10
  • Yes, you were... ;-) But I like the idea to restore the original volume, so when the app is closed (or killed) the user has her volume re-set... – MarcoS Mar 07 '12 at 10:17
  • Just edited it, I don't think you actually need to change the volume you just need to use the max volume rather than the reduced volume when playing it. – zeetoobiker Mar 07 '12 at 10:22
  • I did just try this solution, but I confirm the value returned by getStreamMaxVolume() from SoundPool class reflects the Settins/Audio/Multimedia volume level set by the user. For example, on my device (Gingerbread), a low value of that setting causes a returned value of 15, and the sound played is veeery low... – MarcoS Mar 07 '12 at 21:21
  • You're quite right, I mis-understood the docs, the volume between 0 and 1 for the left and right streams is relative to the current max volume. This means that your original code wasn't quite right as you should be playing with 1 in the streamVolume variable. I've just edited (again - not sure if I should stick this in a new answer but anyway) the question to provide the code I used to achieve the same goal. – zeetoobiker Mar 08 '12 at 14:02
  • if this code doesn't work, don't forget the MODIFY_AUDIO_SETTINGS permission : – Oubaida AlQuraan Oct 28 '16 at 11:35
5

You can adjust the settings before playing the audio.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.getStreamMaxVolume(), 0);
josephus
  • 8,284
  • 1
  • 37
  • 57
2
    float count=100*.01f;

MediaPlayer mp=new MediaPlayer();

 mp.setLooping(false);     
           mp = MediaPlayer.create(ActivityName.this, myUri);

          mp.setVolume(count,count);  
           mp.start(); 
 mp.setOnCompletionListener(new OnCompletionListener() {

            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                                 mp.release(); 
                 mp.stop(); 
            }
        });
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19
1

Both of these codes worked for me but I prefer the one from MediaPlayer

AudioManager  audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);

Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer mp=new MediaPlayer();
mp.setLooping(false);
mp = MediaPlayer.create(HomeActivity.this, notification);
mp.setVolume(count,count);
mp.start();
});
Thilina Anuradh
  • 211
  • 2
  • 11
-1

That's the wrong code.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                             audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
JustAGuy
  • 5,151
  • 11
  • 41
  • 55