Android : How To Set MediaPlayer Volume Programmatically?


Answer :

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


Hope this help

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

For Volume UP

 audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,                         AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); 

for Volume Down

audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,                         AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); 

You do have the setVolume method in the MediaPlayer class. See here


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?