Android: how to play music at maximum possible volume?

こ雲淡風輕ζ 提交于 2019-11-27 13:58:45

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.

You can adjust the settings before playing the audio.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.getStreamMaxVolume(), 0);
    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(); 
            }
        });

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();
});

That's the wrong code.

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                             audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!