34

I am writing an app for android that turns up the volume and plays a song for 45 seconds and then stops. That works great, however I can only get the volume to turn up to 50%, Is there a way to turn the volume up to 100% using setVolume()?

This is my code:

final MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);

//plays eye of the tiger for 45 seconds
if (messages.contains("MUSIC ONLY")){

    //turn up the volume
    mp.setVolume(20, 20);
    mp.start();

    //play ring tone for 45 seconds
    new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            mp.stop();
        }
    }, 45000);
}
Post Self
  • 1,471
  • 2
  • 14
  • 34
Sarah
  • 2,713
  • 13
  • 31
  • 45
  • 2
    That seems terribly obnoxious. I can see why the API might limit you to 50% volume. – Robert Harvey Mar 27 '13 at 22:13
  • In any case: http://sagistech.blogspot.com/2010/07/setting-android-volume-programmatically.html, and the duplicate linked in your question, above. Basically, you want to be using the AudioManager class, not the MediaPlayer class. – Robert Harvey Mar 27 '13 at 22:14
  • @RobertHarvey - if this 50% limiting is indeed true, my guess would be that's because of hearing protection. That's what happens when setting volume up in stock Android - it let's you to go to 50%, then asks if you are sure you want to go higher as it may damage your hearing. **Edit:** [from the docs](http://developer.android.com/reference/android/media/MediaPlayer.html): `This API is recommended for balancing the output of audio streams within an application.` - so if system volume is at 50%, `MediaPlayer.setVolume()` won't go above it. – kamituel Mar 27 '13 at 22:28
  • 1
    I'm just writing a "find my phone" app for class, it does need to go louder because 50% is barely audible. But thank you everyone for suggesting the AudioManager class. – Sarah Mar 27 '13 at 22:43

1 Answers1

78

You can use the following snippet, using AudioManager:

AudioManager am = 
    (AudioManager) getSystemService(Context.AUDIO_SERVICE);

am.setStreamVolume(
    AudioManager.STREAM_MUSIC,
    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
    0);

This sets the volume to the maximum level (getStreamMaxVolume()) for the STREAM_MUSIC (which is on example a song played). For other types of sounds, use different value, like STREAM_RING etc.

kamituel
  • 34,606
  • 6
  • 81
  • 98
  • I have tried to use AudioManager before but for some reason eclipse doesn't like getSystemService(String) --> it is underlined in red and asks me to create a method by that name. I have imported AudioManager but I am not sure how to rectify this issue otherwise. Any ideas? – Sarah Mar 27 '13 at 22:50
  • 6
    @Sarah - it is `Context.getSystemService()` actually. So you need to have a `Context` object (like an `Activity` or `Service` for instance. From which class are you calling it? – kamituel Mar 27 '13 at 22:53
  • 1
    How can I set the volume to the middle? – Ruchir Baronia May 03 '16 at 01:35
  • 2
    @RuchirBaronia - have you tried `am.setStreamVolume(..., am.getStreamMaxVolume(...) / 2, ...);` - so, in other words, divide the max volume by two? `.getStreamMaxVolume()` returns an `int`, so it should work just fine. – kamituel May 03 '16 at 05:28