我在我的服务中使用AudioManager来控制媒体音量,但我只能控制我在setStreamVolume()
中写的媒体类型。我曾尝试使用getRingerMode()
来获取媒体类型,但内容是我的服务。
int currentType = audioManager.getRingerMode();
int currentVoice = audioManager.getStreamVolume(currentType);
audioManager.setStreamVolume(currentType, currentVoice, AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
如何控制其他应用程序卷?
参考:how to increase and decrease the volume programmatically in android
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Button upButton = (Button) findViewById(R.id.upButton);
upButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
});
Button downButton = (Button) findViewById(R.id.downButton);
downButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
});
感谢Dewey Reed和Ashvin solanki。我得到了Ashvin solanki的启发,Dewey Reed帮助我阅读了Android Source。要获取当前媒体类型,可以使用adjustVolume()
并将direction
值设置为AudioManager.ADJUST_SAME
。例如:audioManager.adjustVolume(AudioManager.ADJUST_SAME, AudioManager.FLAG_PLAY_SOUND| AudioManager.FLAG_SHOW_UI);
现在AudioControl窗口显示,并具有当前媒体类型。