// 获取当前音量大小public static int getCurrentVolume(Context context) {AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); // 使用 STREAM_MUSIC 作为示例}// 获取系统最大音量大小public static int getMaxVolume(Context context) {AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 使用 STREAM_MUSIC 作为示例}//设置当前媒体音量是静音还是非静音状态public int setMuteEnabled(boolean isMute) {AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);audioManager.setStreamMute(AudioManager.STREAM_MUSIC, isMute);// Return 1 for mute and 0 for unmutereturn isMute ? 1 : 0;}//获取系统当前媒体音量是静音还是非静音状态public int getCurrentMuteState() {AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);// Check if the stream is mutedboolean isMuted = audioManager.isStreamMute(AudioManager.STREAM_MUSIC);// Return 1 if muted, 0 if not mutedreturn isMuted ? 1 : 0;}//开启静音模式public static void muteSystem(Context context){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (audioManager!=null){audioManager.setStreamMute(AudioManager.STREAM_MUSIC,true);}}//关闭静音模式public static void unMuteSystem(Context context){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (audioManager!=null){audioManager.setStreamMute(AudioManager.STREAM_MUSIC,false);}}//获取当前媒体音量public static int getCurrentVolume(Context context){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (audioManager!=null){return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);}return -1;}//音量增加public static void volumeUp(Context context){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (audioManager!=null){audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);}}//音量降低public static void volumeDown(Context context){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (audioManager!=null){audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);}}//自动调整音量public static void AutoAdjustResetVolume(Context context) {AudioManager mManager= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);if (mManager!= null) {//当前音量int volume = mManager.getStreamVolume(AudioManager.STREAM_MUSIC);try {Thread.sleep(200);} catch (Exception e) {e.printStackTrace();}//与最大音量比较if (volume != mManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {//向音量增加方向调整mManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);mManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);} else {mManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);mManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);}}}
转载请注明出处高通Android 12 音量API设置相关代码-CSDN博客,谢谢!