简体   繁体   中英

Why I am not able to control Media volume when the alert dialog is shown in Android?

I am controlling media volume programmatically from my app. I am setting alert dialog when the media volume is below certain range. When this alert is displayed at this point if I try to adjust the volume controller buttons the ringer volume will get adjusted not media volume. Why is this happening?

I am using these lines to control media volume..

case KeyEvent.KEYCODE_VOLUME_DOWN:

             audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
             return true;
case KeyEvent.KEYCODE_VOLUME_UP:

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

             return true;

Once the alert dialog is dismissed I am able to control the media volume. I am facing this issue only in phone not in tablet.

您可能需要调用Dialog.setVolumeControlStream

How about just letting the phone control the volume on its own? That's pretty much how the designers thought developer-managing-audio-volume . Saves you a lot of trouble. All you have to do is add this line to the onCreate() of each Activity:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

If you want to intercept the volume and add a warning if it goes past a thresh-hold, go ahead. It should still work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM