前言
Android监听音量、修改音量——AudioManager的使用。
方法与参数
设置声音模式:
setRingerMode(int ringerMode)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public static final int RINGER_MODE_SILENT = 0;
public static final int RINGER_MODE_VIBRATE = 1;
public static final int RINGER_MODE_NORMAL = 2;
|
调整声音大小
adjustVolume(int direction, int flags)
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static final int ADJUST_LOWER = -1;
public static final int ADJUST_RAISE = 1;
public static final int FLAG_SHOW_UI = 1 << 0;
public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1;
public static final int FLAG_PLAY_SOUND = 1 << 2;
|
获取最大音量和当前音量
getStreamVolume(int streamType)
和getStreamMaxVolume(int streamType)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public static final int STREAM_VOICE_CALL = 0;
public static final int STREAM_SYSTEM = 1;
public static final int STREAM_MUSIC = 3;
public static final int STREAM_ALARM = 4;
|
调整音量为具体数值
setStreamVolume(int streamType, int index, int flags)
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
public class AudioHelper {
private static AudioHelper instance; private static AudioManager manager;
public static synchronized AudioHelper getInstance() { if (instance == null) { instance = new AudioHelper(); manager = (AudioManager) AppContext.getInstance().getSystemService(Context.AUDIO_SERVICE); } return instance; }
public int getMediaMaxVolume() { return manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); }
public int getMediaVolume() { return manager.getStreamVolume(AudioManager.STREAM_MUSIC); }
public int getCallMaxVolume() { return manager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL); }
public int getCallVolume() { return manager.getStreamVolume(AudioManager.STREAM_VOICE_CALL); }
public int getSystemMaxVolume() { return manager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM); }
public int getSystemVolume() { return manager.getStreamVolume(AudioManager.STREAM_SYSTEM); }
public int getAlarmMaxVolume() { return manager.getStreamMaxVolume(AudioManager.STREAM_ALARM); }
public int getAlarmVolume() { return manager.getStreamVolume(AudioManager.STREAM_ALARM); }
public void setMediaVolume(int volume) { manager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI); }
public void setCallVolume(int volume) { manager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, volume, AudioManager.STREAM_VOICE_CALL); }
public void setAlarmVolume(int volume) { manager.setStreamVolume(AudioManager.STREAM_ALARM, volume, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI); }
public void setSystemVolume(int volume) { manager.setStreamVolume(AudioManager.STREAM_SYSTEM, volume, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI); }
public void setSpeakerStatus(boolean on) {
if (on) { manager.setSpeakerphoneOn(true); manager.setMode(AudioManager.MODE_NORMAL); } else { manager.setSpeakerphoneOn(false); manager.setMode(AudioManager.MODE_IN_COMMUNICATION);
} } }
|
------------本文结束感谢您的阅读------------
Thank you for your accept!
微信支付
支付宝