How do you get/set media volume (not ringtone volume) in Android?

前端 未结 8 1834
青春惊慌失措
青春惊慌失措 2020-12-01 00:01

Is there a way to get/set media volume? I have tried the following:

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentV         


        
相关标签:
8条回答
  • 2020-12-01 00:41
    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);
    
    0 讨论(0)
  • 2020-12-01 00:49

    The following code will set the media stream volume to max:

    AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
        audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
        AudioManager.FLAG_SHOW_UI);
    
    0 讨论(0)
提交回复
热议问题