//AudioManager的实例化
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
快速介绍
AudioManager.java提供的音量相关接口
void setStreamVolume(int streamType, int index, int flags): 调节所给 streamType 指定的STREAM音量,音量值是index
void adjustStreamVolume(int streamType, int direction, int flags):调节streamType 指定的STREAM的音量,没有指定音量值,direction指明是往上调、往下调、或者静音、非静音
void adjustSuggestedStreamVolume (int direction, int suggestedStreamType, int flags):调节当前最相关STREAM音量,例如默认调节来电铃声音量,播放音乐时,调节音乐的音量;也是往上调、往下调。按音量键调节音量,就是调用这个函数
void adjustVolume(int direction, int flags):跟adjustSuggestedStreamVolume()的功能类似
int getStreamVolume (int streamType):获取所给 streamType 的当前音量
1.adjustVolume(int direction, int flags)
//控制手机音量,调大或者调小一个单位,根据第一个参数进行判断
// AudioManager.ADJUST_LOWER,可调小一个单位; AudioManager.ADJUST_RAISE,可调大一个单位
public void adjustVolume(int direction, int flags) {
Log.d(TAG, "adjustVolume: Flags = " + flags + ", direction = " + direction);
MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
helper.sendAdjustVolumeBy(USE_DEFAULT_STREAM_TYPE, direction, flags);
}
2.adjustStreamVolume(int streamType, int direction, int flags)
/*
参数1:streamType参数,指定声音类型,有下述几种声音类型:
STREAM_ALARM:手机闹铃 STREAM_MUSIC:手机音乐
STREAM_RING:电话铃声 STREAM_SYSTEAM:手机系统
STREAM_DTMF:音调 STREAM_NOTIFICATION:系统提示
STREAM_VOICE_CALL:语音电话
参数2:上面那个一样,调大或调小音量的。
参数3:可选的标志位,比如AudioManager.FLAG_SHOW_UI,显示进度条,AudioManager.PLAY_SOUND:播放声音。
**/
public void adjustStreamVolume(int streamType, int direction, int flags) {
final IAudioService service = getService();
Log.d(TAG, "adjustStreamVolume: StreamType = " + streamType + ", direction = " + direction);
try {
//调用AudioService
service.adjustStreamVolume(streamType, direction, flags,
getContext().getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
3.插播AudioManager 封装了AudioService的服务,并通过getservice 获取到AudioService的 Service 实例。
private static IAudioService getService()
{
if (sService != null) {
return sService;
}
IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
sService = IAudioService.Stub.asInterface(b);
return sService;
}
4.setStreamVolume(int streamType, int index, int flags) 直接设置音量
/**
* Sets the volume index for a particular stream.
* <p>This method has no effect if the device implements a fixed volume policy
* as indicated by {@link #isVolumeFixed()}.
* <p>From N onward, volume adjustments that would toggle Do Not Disturb are not allowed unless
* the app has been granted Do Not Disturb Access.
* See {@link NotificationManager#isNotificationPolicyAccessGranted()}.
* @param streamType The stream whose volume index should be set.
* @param index The volume index to set. See
* {@link #getStreamMaxVolume(int)} for the largest valid value.
* @param flags One or more flags.
* @see #getStreamMaxVolume(int)
* @see #getStreamVolume(int)
* @see #isVolumeFixed()
*/
public void setStreamVolume(int streamType, int index, int flags) {
final IAudioService service = getService();
Log.d(TAG, "setStreamVolume: StreamType = " + streamType + ", index = " + index);
try {
service.setStreamVolume(streamType, index, flags, getContext().getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
5.setMode(int mode)
/**
* Sets the audio mode.
* <p>
* The audio mode encompasses audio routing AND the behavior of
* the telephony layer. Therefore this method should only be used by applications that
* replace the platform-wide management of audio settings or the main telephony application.
* In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony
* application when it places a phone call, as it will cause signals from the radio layer
* to feed the platform mixer.
*
* @param mode the requested audio mode ({@link #MODE_NORMAL}(普通), {@link #MODE_RINGTONE(铃声)},
* {@link #MODE_IN_CALL}(打电话) or {@link #MODE_IN_COMMUNICATION}(通话)).
* Informs the HAL about the current audio state so that
* it can route the audio appropriately.
*/
public void setMode(int mode) {
final IAudioService service = getService();
try {
service.setMode(mode, mICallBack, mApplicationContext.getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
6.getMode()获取当前音频模式
/**
* Returns the current audio mode.
*
* @return the current audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE},
* {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}).
* Returns the current current audio state from the HAL.
*/
public int getMode() {
final IAudioService service = getService();
try {
return service.getMode();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
7.设置铃声模式
/**
* Sets the ringer mode.xiangzaixiansheng
* <p>
* Silent mode will mute the volume and will not vibrate. Vibrate mode will
* mute the volume and vibrate. Normal mode will be audible and may vibrate
* according to user settings.
* <p>This method has no effect if the device implements a fixed volume policy
* as indicated by {@link #isVolumeFixed()}.
* * <p>From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed
* unless the app has been granted Do Not Disturb Access.
* See {@link NotificationManager#isNotificationPolicyAccessGranted()}.
* @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL}(普通模式),
* {@link #RINGER_MODE_SILENT}(静音模式), or {@link #RINGER_MODE_VIBRATE}(震动模式).
* @see #getRingerMode()
* @see #isVolumeFixed()
*/
public void setRingerMode(int ringerMode) {
if (!isValidRingerMode(ringerMode)) {
return;
}
final IAudioService service = getService();
try {
service.setRingerModeExternal(ringerMode, getContext().getOpPackageName());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
8.获取手机音量值(0-7)之间,streamType The stream type whose volume index is returned.
/**
* Returns the current volume index for a particular stream.
*xiangzaixiansheng
* @param streamType The stream type whose volume index is returned.
* @return The current volume index for the stream.
* @see #getStreamMaxVolume(int)
* @see #setStreamVolume(int, int, int)
*/
public int getStreamVolume(int streamType) {
final IAudioService service = getService();
try {
return service.getStreamVolume(streamType);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
9.getStreamMaxVolume(int streamType) 获取最大音量值
/**
* Returns the maximum volume index for a particular stream.
*
* @param streamType The stream type whose maximum volume index is returned.
* @return The maximum valid volume index for the stream.
* @see #getStreamVolume(int)
*/
public int getStreamMaxVolume(int streamType) {
final IAudioService service = getService();
try {
return service.getStreamMaxVolume(streamType);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
10.setStreamMute(int streamType, boolean state)设置静音模式。
/**
* Mute or unmute an audio stream.
* <p>
* This method should only be used by applications that replace the
* platform-wide management of audio settings or the main telephony
* application.
* <p>
* This method has no effect if the device implements a fixed volume policy
* as indicated by {@link #isVolumeFixed()}.
* <p>
* This method was deprecated in API level 22. Prior to API level 22 this
* method had significantly different behavior and should be used carefully.
* The following applies only to pre-22 platforms:
* <ul>* <li>The mute command is protected against client process death: if a
* process with an active mute request on a stream dies, this stream will be
* unmuted automatically.</li>
* <li>The mute requests for a given stream are cumulative: the AudioManager
* can receive several mute requests from one or more clients and the stream
* will be unmuted only when the same number of unmute requests are
* received.</li>
* <li>For a better user experience, applications MUST unmute a muted stream
* in onPause() and mute is again in onResume() if appropriate.</li>
* </ul>
* * @param streamType The stream to be muted/unmuted.
* @param state The required mute state: true for mute ON, false for mute
* OFF
* @see #isVolumeFixed()
* @deprecated Use {@link #adjustStreamVolume(int, int, int)} with
* {@link #ADJUST_MUTE} or {@link #ADJUST_UNMUTE} instead.
*/
@Deprecated
public void setStreamMute(int streamType, boolean state) {
Log.w(TAG, "setStreamMute is deprecated. adjustStreamVolume should be used instead.");
int direction = state ? ADJUST_MUTE : ADJUST_UNMUTE;
if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {
adjustSuggestedStreamVolume(direction, streamType, 0);
} else {
adjustStreamVolume(streamType, direction, 0);
}
}
11.判断麦克风是否静音isMicrophoneMute()
/**
* Checks whether the microphone mute is on or off.
*xiangzaixiansheng
* @return true if microphone is muted, false if it's not
*/
public boolean isMicrophoneMute() {
return AudioSystem.isMicrophoneMuted();
}
12.setMicrophoneMute(boolean on)设置麦克风为静音
/**
* Sets the microphone mute on or off.
* <p>
* This method should only be used by applications that replace the platform-wide
* management of audio settings or the main telephony application.
*
* @param on set <var>true</var> to mute the microphone;
* <var>false</var> to turn mute off
*/
public void setMicrophoneMute(boolean on) {
final IAudioService service = getService();
try {
service.setMicrophoneMute(on, getContext().getOpPackageName(),
UserHandle.getCallingUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
13.isWiredHeadsetOn()判断是否插入耳机
/**
* Checks whether a wired headset is connected or not.
* <p>This is not a valid indication that audio playback is
* actually over the wired headset as audio routing depends on other conditions.
*
* @return true if a wired headset is connected.
* false if otherwise
* @deprecated Use {@link AudioManager#getDevices(int)} instead to list available audio devices.
*/
public boolean isWiredHeadsetOn() {
if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"")
== AudioSystem.DEVICE_STATE_UNAVAILABLE &&
AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"")
== AudioSystem.DEVICE_STATE_UNAVAILABLE &&
AudioSystem.getDeviceConnectionState(DEVICE_OUT_USB_HEADSET, "")
== AudioSystem.DEVICE_STATE_UNAVAILABLE) {
return false;
} else {
return true;
}
}
14.判断音乐是否处在活跃模式
/**
* Checks whether any music is active.
*
* @return true if any music tracks are active.
*/
public boolean isMusicActive() {
return AudioSystem.isStreamActive(STREAM_MUSIC, 0);
}