title: how to turn speaker on/off programmatically in android 4.0
body: <p>I play a file through media player and I want to give options like speaker on/off, play though headset, bluetooth ,etc.
  I tried the below code which works well for android 2.2 but I want something that can also work for 2.2 and 4.0 both.
  Can you help me to programmatically turn the speaker on/off and playing via headphones?</p>
  
  <pre><code>AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
      if(isOn){
          audioManager.setMode(AudioManager.MODE_IN_CALL);    
          audioManager.setMode(AudioManager.MODE_NORMAL); 
      }else{
          //Seems that this back and forth somehow resets the audio channel
          audioManager.setMode(AudioManager.MODE_NORMAL);     
          audioManager.setMode(AudioManager.MODE_IN_CALL);        
      }
      audioManager.setSpeakerphoneOn(isOn);
  </code></pre>
  
  <p>P.S: I have given this permission in manifest:</p>
  
  <pre><code>android.permission.MODIFY_AUDIO_SETTINGS 
  </code></pre>