Code example for AudioManager

Methods: getStreamMaxVolumegetStreamVolumesetStreamVolume

0
    { 
        if(!ignoreNext)
        { 
            AudioManager am =
                (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
            int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            int maxVolRinger = am.getStreamMaxVolume(AudioManager.STREAM_RING);
            VolumeSettings settings = new VolumeSettings(context);
            int state = processIntent(context, intent,
                settings.getBluetoothDetectionEnabled());
            switch(state)
            { 
            // unplugged. 
                case 0: 
                { 
                    int newVol = (int)(maxVol * settings.getUnpluggedLevel());
                    int newVolRinger = (int)(maxVolRinger *
                        settings.getUnpluggedLevelRinger());
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, newVol,
                        AudioManager.FLAG_SHOW_UI);
                    if(settings.getEnableRingerControl())
                    { 
                        am.setStreamVolume(AudioManager.STREAM_RING, newVolRinger,
                            AudioManager.FLAG_SHOW_UI);
                    } 
                    break; 
                } 
                // plugged in 
                case 1: 
                { 
                    if(settings.getSaveUnplugLevel())
                    { 
                        float oldVol =
                            (float)am.getStreamVolume(AudioManager.STREAM_MUSIC) / maxVol;
                        float oldVolRinger =
                            (float)am.getStreamVolume(AudioManager.STREAM_RING) /
                                maxVolRinger;
                        settings.setUnpluggedLevel(oldVol);
                        settings.setUnpluggedLevelRinger(oldVolRinger);
                    } 
                    int newVol = (int)(maxVol * settings.getPluggedLevel());
                    int newVolRinger =
                        (int)(maxVolRinger * settings.getPluggedLevelRinger());
                    am.setStreamVolume(AudioManager.STREAM_MUSIC, newVol,
                        AudioManager.FLAG_SHOW_UI);
                    if(settings.getEnableRingerControl())
                    { 
                        am.setStreamVolume(AudioManager.STREAM_RING, newVolRinger,
                            AudioManager.FLAG_SHOW_UI);
                    } 
                    break; 
                } 
            }