下面列出了android.media.AudioManager#getStreamMaxVolume ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean shouldUpdateAudioVolume(boolean mute) {
// Do nothing if in mute.
if (mute) {
return true;
}
// Update audio status if current volume position is edge of volume bar,
// i.e max or min volume.
AudioManager audioManager = tv().getService().getAudioManager();
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
if (mIsVolumeUp) {
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
return currentVolume == maxVolume;
} else {
return currentVolume == 0;
}
}
private void soundAlert(String soundUri) {
manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
Uri notification = Uri.parse(soundUri);
MediaPlayer player = MediaPlayer.create(mContext, notification);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
manager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
}
});
player.start();
}
public static void soundAlert(String soundUri) {
manager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, 0);
Uri notification = Uri.parse(bg_notification_sound);
MediaPlayer player = MediaPlayer.create(mContext, notification);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
manager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
}
});
player.start();
}
@Override
public void onChange (boolean selfChange)
{
if (MainActivity.mainActivity == null) return;
//
float minVolume = OBConfigManager.sharedManager.getMinimumAudioVolumePercentage() / (float) 100;
// if value is not present in the config, the min volume will be -.01
AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE);
currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int minVolumeLimit = Math.round(maxVolume * minVolume);
if (currentVolume < minVolumeLimit)
{
MainActivity.log("Current Volume (" + currentVolume + ") lower than permitted minimum (" + minVolumeLimit + "). Resetting value");
am.setStreamVolume(AudioManager.STREAM_MUSIC, minVolumeLimit, 0);
currentVolume = minVolumeLimit;
}
OBAnalyticsManager.sharedManager.deviceVolumeChanged(currentVolume / (float) maxVolume);
OBSystemsManager.sharedManager.refreshStatus();
}
@Override
public void onAudioFocusChange(int focusChange) {
Log.e(TAG, "audioFocusChangeListener.onAudioFocusChange>>>>>>>>>>>>>>>>>>" + focusChange);
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
if (mPlayer != null) {
volScalar = ((float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)) / audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// Log.i("LingJu", "获取媒体音量:" + volScalar);
mPlayer.setVolume(0.2f, 0.2f);
}
break;
}
case AudioManager.AUDIOFOCUS_GAIN:
if (mPlayer != null) {
// Log.i("LingJu", "获取焦点后设置播放音量:" + volScalar);
volScalar = volScalar == 0 ? 0.5f : volScalar;
mPlayer.setVolume(volScalar, volScalar);
}
break;
default:
break;
}
}
/**
* 调整音量
*
* @param increase true=增加百分之20 false=减少百分之20
*/
public String ajustVol(boolean increase, int progress) {
AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
float max = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = (int) (currentVolume + max * (progress / 100.0f) * (increase ? 1 : -1));
if (currentVolume < 0)
currentVolume = 0;
else if (currentVolume > max)
currentVolume = (int) max;
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
//弹出系统媒体音量调节框
mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_SAME, AudioManager.FLAG_PLAY_SOUND
| AudioManager.FLAG_SHOW_UI);
NumberFormat nf = NumberFormat.getPercentInstance();
//返回数的整数部分所允许的最大位数
nf.setMaximumIntegerDigits(3);
//返回数的小数部分所允许的最大位数
// nf.setMaximumFractionDigits(2);
return nf.format(currentVolume / max);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// assume that the user cancels the call if it is ringing
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (!canCallBeResumed()) {
if (null != mCall) {
mCall.hangup("");
}
} else {
saveCallView();
}
} else if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
// this is a trick to reduce the ring volume :
// when the call is ringing, the AudioManager.Mode switch to MODE_IN_COMMUNICATION
// so the volume is the next call one whereas the user expects to reduce the ring volume.
if ((null != mCall) && mCall.getCallState().equals(IMXCall.CALL_STATE_RINGING)) {
AudioManager audioManager = (AudioManager) CallViewActivity.this.getSystemService(Context.AUDIO_SERVICE);
// IMXChrome call issue
if (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION) {
int musicVol = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL) * audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, musicVol, 0);
}
}
}
return super.onKeyDown(keyCode, event);
}
private void init(Context context) {
mActivity = (Activity) context;
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
View root = LayoutInflater.from(context).inflate(R.layout.lib_player_layout_touch, this);
initView(root);
int[] size = Util.getScreenSize(mActivity);
screenWidth = size[0];
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
gestureDetector = new GestureDetector(context, new PlayerGestureListener());
root.setClickable(true);
root.setOnTouchListener(this);
}
/**
* 状態変化のイベントを通知.
*
* @param status ステータス
*/
public void sendOnStatusChangeEvent(final String status) {
if (mOnStatusChangeEventFlag) {
List<Event> events = EventManager.INSTANCE.getEventList(HostDevicePlugin.SERVICE_ID,
MediaPlayerProfile.PROFILE_NAME, null, MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);
AudioManager manager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
double maxVolume = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
double mVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
double mVolumeValue = mVolume / maxVolume;
for (int i = 0; i < events.size(); i++) {
Event event = events.get(i);
Intent intent = EventManager.createEventMessage(event);
MediaPlayerProfile.setAttribute(intent, MediaPlayerProfile.ATTRIBUTE_ON_STATUS_CHANGE);
Bundle mediaPlayer = new Bundle();
MediaPlayerProfile.setStatus(mediaPlayer, status);
MediaPlayerProfile.setMediaId(mediaPlayer, mMyCurrentMediaId);
MediaPlayerProfile.setMIMEType(mediaPlayer, mMyCurrentFileMIMEType);
MediaPlayerProfile.setPos(mediaPlayer, mMyCurrentMediaPosition / UNIT_SEC);
MediaPlayerProfile.setVolume(mediaPlayer, mVolumeValue);
MediaPlayerProfile.setMediaPlayer(intent, mediaPlayer);
sendEvent(intent, event.getAccessToken());
}
}
}
/**
* 获取指定声音流最大音量大小
* @param streamType 流类型
* @return 最大音量大小
*/
public static int getStreamMaxVolume(final int streamType) {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
return audioManager.getStreamMaxVolume(streamType);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getStreamMaxVolume");
}
}
return 0;
}
private void startRing(Context context, Intent intent) {
Log.d(TAG,"startRing()");
if (currentRingtone != null) {
currentRingtone.stop();
currentRingtone = null;
}
final String address = intent.getStringExtra(Devices.ADDRESS);
final String source = intent.getStringExtra(Devices.SOURCE);
Uri sound = Uri.parse(Preferences.getRingtone(context, address, source));
currentRingtone = RingtoneManager.getRingtone(context, sound);
if (currentRingtone == null) {
Toast.makeText(context, R.string.ring_tone_not_found, Toast.LENGTH_LONG).show();
return;
}
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int max = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_RING, max, 0);
currentRingtone.play();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new Notification.Builder(context)
.setContentText(context.getString(R.string.stop_ring))
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(context, ToggleRingPhone.class), PendingIntent.FLAG_UPDATE_CURRENT))
.build();
notificationManager.notify(NOTIFICATION_ID, notification);
}
public void play(int sound) {
AudioManager mgr = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr
.getStreamVolume(AudioManager.STREAM_RING);
float streamVolumeMax = mgr
.getStreamMaxVolume(AudioManager.STREAM_RING);
float volume = streamVolumeCurrent / streamVolumeMax;
sounds.play(soundsMap.get(sound), volume, volume, 1, 0, 1.f);
}
public boolean allowsLowerVolume ()
{
if (MainActivity.mainActivity == null) return false;
//
float minVolume = OBConfigManager.sharedManager.getMinimumAudioVolumePercentage() / (float) 100;
// if value is not present in the config, the min volume will be -.01
AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE);
currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int minVolumeLimit = Math.round(maxVolume * minVolume);
return currentVolume > minVolumeLimit;
}
public String printVolumeStatus ()
{
if (MainActivity.mainActivity == null) return "";
//
AudioManager am = (AudioManager) MainActivity.mainActivity.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//
return Math.round((currentVolume / (float) maxVolume) * 100.0f) + "%";
}
@Override
public void changeMediaVolume(int percent) {
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mCurrentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int volume = (int) ((percent / 100.0) * maxVolume + 0.5);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_SAME, AudioManager.FLAG_PLAY_SOUND
| AudioManager.FLAG_SHOW_UI);
}
public void volumeDown() {
AudioManager am =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am == null) return;
int currVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
currVolume = currVolume - (maxVolume / 10);
if (currVolume < 0)
currVolume = 0;
am.setStreamVolume(AudioManager.STREAM_MUSIC, currVolume, AudioManager.FLAG_SHOW_UI);
}
private static int getVolumeIndexFromPercentage(AudioManager audioManager, int streamType, float volumePercentage) {
int maxIndex = audioManager.getStreamMaxVolume(streamType);
return Math.round(volumePercentage / 100f * (float) maxIndex);
}
private void initAudioManager(Context context) {
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}
@Override
public void adjustVolumeLevel(double adjustPercent) {
if (disableGestures) {
return;
}
// We are setting volume percent to a value that should be from -1.0 to 1.0. We need to limit it here for these values first
if (adjustPercent < -1.0f) {
adjustPercent = -1.0f;
} else if (adjustPercent > 1.0f) {
adjustPercent = 1.0f;
}
AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
final int STREAM = AudioManager.STREAM_MUSIC;
// Max volume will return INDEX of volume not the percent. For example, on my device it is 15
int maxVolume = audioManager.getStreamMaxVolume(STREAM);
if (maxVolume == 0) return;
if (startVolumePercent < 0) {
// We are getting actual volume index (NOT volume but index). It will be >= 0.
int curVolume = audioManager.getStreamVolume(STREAM);
// And counting percents of maximum volume we have now
startVolumePercent = curVolume * 1.0f / maxVolume;
}
// Should be >= 0 and <= 1
double targetPercent = startVolumePercent + adjustPercent;
if (targetPercent > 1.0f) {
targetPercent = 1.0f;
} else if (targetPercent < 0) {
targetPercent = 0;
}
// Calculating index. Test values are 15 * 0.12 = 1 ( because it's int)
int index = (int) (maxVolume * targetPercent);
if (index > maxVolume) {
index = maxVolume;
} else if (index < 0) {
index = 0;
}
audioManager.setStreamVolume(STREAM, index, 0);
indicatorImageView.setImageResource(R.drawable.ic_volume);
indicatorTextView.setText(index * 100 / maxVolume + "%");
// Show indicator. It will be hidden once onGestureDone will be called
showIndicator();
}
/**
* Get the maximum volume value of the media stream
*
* @param audioManager object
* @return the integer value
*/
private static int getMaxMediaVolume(@NonNull final AudioManager audioManager) {
return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
}