有没有办法获取/设置媒体量?我尝试过以下方法:
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
但它会返回铃声音量。
答案 0 :(得分:59)
private AudioManager audio;
在onCreate中:
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
覆盖onKeyDown:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
答案 1 :(得分:46)
而不是AudioManager.STREAM_RING
您应该使用AudioManager.STREAM_MUSIC
这个问题已经讨论过here。
答案 2 :(得分:31)
试试这个:
setVolumeControlStream(AudioManager.STREAM_MUSIC);
答案 3 :(得分:27)
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float percent = 0.7f;
int seventyVolume = (int) (maxVolume*percent);
audio.setStreamVolume(AudioManager.STREAM_MUSIC, seventyVolume, 0);
答案 4 :(得分:17)
您可以将活动设置为使用特定卷。在您的活动中,使用以下其中一项:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
this.setVolumeControlStream(AudioManager.STREAM_RING);
this.setVolumeControlStream(AudioManager.STREAM_ALARM);
this.setVolumeControlStream(AudioManager.STREAM_NOTIFICATION);
this.setVolumeControlStream(AudioManager.STREAM_SYSTEM);
this.setVolumeControlStream(AudioManager.STREAM_VOICECALL);
答案 5 :(得分:6)
如果你碰巧有一个你想调整的音量条 - 与你在iPhone的iPod应用程序上看到的相似 - 这就是如何。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
//Raise the Volume Bar on the Screen
volumeControl.setProgress( audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
+ AudioManager.ADJUST_RAISE);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//Adjust the Volume
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
//Lower the VOlume Bar on the Screen
volumeControl.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC)
+ AudioManager.ADJUST_LOWER);
return true;
default:
return false;
}
答案 6 :(得分:5)
以下代码将媒体流量设置为max:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
AudioManager.FLAG_SHOW_UI);
答案 7 :(得分:4)
将音量设为0
AudioManager audioManager;
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
将音量设为完整
AudioManager audioManager;
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
可以通过更改0到20之间的索引值来调整音量