Setting the Android System Volume

大城市里の小女人 提交于 2020-01-29 09:33:25

问题


I am writing an application which handles the following:

  1. Silent - Makes the phone volume silent
  2. Low - Makes the phone volume 1 point above the silent
  3. Medium - Makes the phone volume medium
  4. High - Makes the phone volume maximum

Phone volume includes Ringer, video, audio , games volume etc.

I know how to achieve Silent mode through AudioManager. But for 2-4 , I could not find any helpful code snippets. What do I need to do?


回答1:


Add this in onCreate() method of your code and then play around with the 0 value to increase or decrease the volume where 0 means OFF.

AudioManager mgr=null;

mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);



回答2:


Check out http://thinkandroid.wordpress.com/2010/04/05/dealing-with-the-android-media-player/

The steps are:

  • get the audio man.
  • fetch the required stream.
  • call appropriate method on stream.

e.g.:

mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
  • to get max vol:

    int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    


来源:https://stackoverflow.com/questions/9164347/setting-the-android-system-volume

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