Code example for AudioManager

Methods: getRingerModegetStreamVolumesetStreamVolume

0
     * update volume using device's manner mode settings and head-set plug status 
     */ 
    public void setMannerMode() 
    { 
    	AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		int mediaVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		int mode = audioManager.getRingerMode();
		boolean bManner = (mode == AudioManager.RINGER_MODE_SILENT || mode == AudioManager.RINGER_MODE_VIBRATE);
 
		if (isMannerModeOn == bManner && isWearingHeadset == headsetEquipped) {
			return; 
		} 
		 
		if (!isMannerModeOn && bManner) {
			// manner mode turned on 
			if (!isWearingHeadset && headsetEquipped) {
				// head-set on 
				m_mediaVolume = DEFAULT_VOLUME;
			} 
			else if (isWearingHeadset && headsetEquipped) {
				// head-set kept on 
				m_mediaVolume = mediaVolume;
			} 
			else if ((!isWearingHeadset && !headsetEquipped) || (isWearingHeadset && !headsetEquipped)) {
				// head-set kept off or turned off 
				m_mediaVolume = 0;
			} 
		} 
		else if (isMannerModeOn && !bManner) {
			// manner mode turned off 
			if (!isWearingHeadset && headsetEquipped) {
				// head-set turned on 
				m_mediaVolume = DEFAULT_VOLUME;
			} 
			else if (isWearingHeadset == headsetEquipped) {
				// head-set kept (on or off) 
				m_mediaVolume = mediaVolume;
			} 
			else { 
				// head-set turned off 
				// this setting could be dependent on game policy 
				m_mediaVolume = 0;
			} 
		} 
		else { 
			if (isMannerModeOn) {
				// manner mode kept on 
				if (!isWearingHeadset && headsetEquipped) {
					// head-set turned on 
					m_mediaVolume = DEFAULT_VOLUME;
				} 
				else if (isWearingHeadset == headsetEquipped) {
					// head-set kept (on or off) 
					m_mediaVolume = mediaVolume;
				} 
				else { 
					// head-set turned off 
					m_mediaVolume = 0;
				} 
			} 
			else if (!isMannerModeOn) {
				// manner mode kept still off 
				if (!isWearingHeadset && headsetEquipped) {
					// head-set turned on 
					m_mediaVolume = DEFAULT_VOLUME;
				} 
				else if (isWearingHeadset == headsetEquipped) {
					// head-set kept (on or off) 
					m_mediaVolume = mediaVolume;
				} 
				else { 
					// head-set turned off 
					m_mediaVolume = 0;
				} 
			} 
		} 
		if (m_mediaVolume != mediaVolume) {
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, m_mediaVolume, 0);
		} 
		isMannerModeOn = bManner;
		isWearingHeadset = headsetEquipped;
    }