How to Get/Set Media Volume (Not Ringtone Volume) in Android

How do you get/set media volume (not ringtone volume) in Android?

Instead of AudioManager.STREAM_RING you shoul use AudioManager.STREAM_MUSIC
This question has already discussed here.

MediaPlayer - use ringtone volume instead of media volume

Use setAudioStreamType(int) to set the media type to type STREAM_RING, then it should use the ringer volume instead of the default STREAM_MUSIC.

Note that you must do this before the media is prepared, so you'll have to prepare it manually with setDataSource instead of using MediaPlayer.create().

How to set android MediaPlayer volume according to ringtone volume?

Instead of adjusting the volume, you should use setAudioStreamType() to set which audio stream you want to play your audio over - this automatically uses the volume of the selected stream. For example, if you want your audio to play at the same volume as a notification would, you could use AudioManager.STREAM_NOTIFICATION:

mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);

Change Media volume in Android?

The right method to use would be setStreamVolume on your AudioManager. It could looks like this

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

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
[int value],
[if desired a flag]);

An example use of the flag is to get the beep when setting the volume so the user can hear the outcome. The flag for that would be AudioManager.FLAG_PLAY_SOUND.

You could use AudioManager.FLAG_SHOW_UI if you don't want to play a sound but display a toast with the current value. The use has to get a feedback tho. Doesn't matter if it is audible or visual.

To get the maximal valid value for the given stream you just call getStreamMaxVolume() on the AudioManager and get an integer back which represents ... well the maximal valid value for the volume.

How to increase the ringer & notification volume programmatically in android

Replace the line

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

with below line

mobilemode.setStreamVolume(AudioManager.STREAM_RING,audioManager.getStreamMaxVolume(AudioManager.STREAM_RING),0);

Android: How do you play a sound at a volume unaffected by any of the Android system volumes (ringtone, media, etc)?

I believe most apps aiming to do this choose a stream type, stash the current volume level (using AudioManager.getStreamVolume), set that volume to whatever they want it to be (using setStreamVolume() to some percentage of getStreamMaxVolume), and then restore the stashed volume level after playback completes. But think hard before you do this -- unless you're creating a "find my lost phone" app, there's rarely a reason to ignore the user's requested volume levels, and you run a high risk of irritating someone silencing their phone for a meeting.

How to set Soundmanager play volume at ringtone volume?

As per Michael, I have to change this in "initSounds" in my SoundManager also. Thanks Michael.

Volume Control in android application

There was another question from a long time ago that asked the same thing. Essentially the answer is: don't override the onKeyDown and onKeyUp buttons. It's much better to simply use this one line setVolumeControlStream(AudioManager.STREAM_MUSIC); in your onCreate() method. That tells the OS that the volume buttons should affect the "media" volume when your application is visible, and that's the volume it uses for your application.

As for controlling the Media volume no matter what app is visible, I'm not sure that can be done - or if it could, whether that would be a good thing.



Related Topics



Leave a reply



Submit