Hi!
I wish to know if there is a way, using which we can switch between the speaker and headset dynamically in an android application. I am using this sample code, I found online for my experiments 
final float frequency = 440;
float increment = (float)(2*Math.PI) * frequency / 44100; // angular increment for each sample
float angle = 0;
AndroidAudioDevice device = new AndroidAudioDevice( );
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
float samples[] = new float[1024];
int count = 0;
while( count < 10 )
{
    count++;
    for( int i = 0; i < samples.length; i++ )
    {
        samples[i] = (float)Math.sin( angle ) ;
        angle += increment;
    }
    device.writeSamples( samples );
}
device.stop();
am.setMode(AudioManager.MODE_NORMAL);
---- next class
public class AndroidAudioDevice
{
   AudioTrack track;
   short[] buffer = new short[1024];
   public AndroidAudioDevice( )
   {
      int minSize =AudioTrack.getMinBufferSize( 44100, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT );        
      track = new AudioTrack( AudioManager.STREAM_VOICE_CALL, 44100, 
                                        AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, 
                                        minSize, AudioTrack.MODE_STREAM);
      track.play();        
   }    
   public void writeSamples(float[] samples) 
   { 
      fillBuffer( samples );
      track.write( buffer, 0, samples.length );
   }
   private void fillBuffer( float[] samples )
   {
      if( buffer.length < samples.length )
         buffer = new short[samples.length];
      for( int i = 0; i < samples.length; i++ )
         buffer[i] = (short)(samples[i] * Short.MAX_VALUE);;
   } 
   public void stop()
   {
    track.stop();
   }
}
As per my understanding this should play audio on headset, because we have not enabled the speaker phone. However, the audio is playing on the speaker phone.
1 Am I doing something wrong here?
2 What would be a way to switch between internal speaker and speaker phone dynamically for same code peice
Any help will be appreciated.