// 获取当前音量大小
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 unmute
return isMute ? 1 : 0;
}
//获取系统当前媒体音量是静音还是非静音状态
public int getCurrentMuteState() {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
// Check if the stream is muted
boolean isMuted = audioManager.isStreamMute(AudioManager.STREAM_MUSIC);
// Return 1 if muted, 0 if not muted
return 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博客,谢谢!
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 高通Android 12 音量API设置相关代码
发表评论 取消回复