如何在Android中获取/设置媒体音量(不是铃声音量)?

有没有办法获得/设置媒体音量? 我已经尝试了以下内容:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING); 

但它会返回铃声音量。

而不是AudioManager.STREAM_RING你使用AudioManager.STREAM_MUSIC这个问题已经在这里讨论。

 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; } } 

试试这个:

setVolumeControlStream(AudioManager.STREAM_MUSIC);

 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); 

您可以将您的活动设置为使用特定的音量。 在您的活动中,请使用以下任一项目:

 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); 

如果你碰巧有一个音量栏,你想调整 – 类似于你的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; } 

以下代码将媒体stream音量设置为最大值:

 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI); 

将音量设置为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之间的索引值来调整音量