Code example for AudioManager

Methods: getStreamMaxVolumegetStreamVolumesetStreamVolume

0
 
    public boolean toggleMute() { 
        isMuted = isVolumeMuted();
        if (isMuted) {
            //If muted, sets the volume at max/2 to avoid making headphone users go deaf. 
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) / 2, 0);
        } else { 
            //If not muted, mutes the volume. 
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
        } 
 
        isMuted = !isMuted;
        setVolumeToggleImage(isMuted);
 
 
        return isMuted;
    } 
 
    //Sets the appropriate image graphic based on whether the volume is muted or not. 
    private void setVolumeToggleImage(boolean isMute) {
        toggleImage.setImageResource(isMute ? R.drawable.ic_audio_vol_mute : R.drawable.ic_audio_vol);
    } 
 
    private boolean isVolumeMuted() { 
        if (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) == 0) {
            return true; 
        } 
        return false; 
    } 
 
Stop searching for code, let great code find you!  Add Codota to your java IDE