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?
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);
P.S: I have given this permission in manifest:
android.permission.MODIFY_AUDIO_SETTINGS