Hello. I am writing an application that uses TextToSpeech to output sound. When you turn on the headphones in the connector, the sound continues to go from the regular speakers, ignoring this inclusion. Actually the question is: is it necessary to somehow change something programmatically so that the sound goes into the headphones or everything should happen by itself? I did not find the answer to my question on the Internet.

    2 answers 2

    The isSpeakerphoneOn () method allows you to determine whether music is playing through the speakers.

    if (audioManager.isSpeakerphoneOn()) { // disable speakerphone button } 

    You can switch to the speakers or disable them via the setSpeakerphoneOn () method:

     audioManager.setSpeakerphoneOn(true); 

    Read more: http://developer.alexanderklimov.ru/android/theory/audiomanager.php

    • Thanks for the answer. I saw this article. In my program, the audioManager.setSpeakerphoneOn(true); or audioManager.setSpeakerphoneOn(false); did not affect anything at all. I get audioManager like this: AudioManager audioManager= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - Sergey
    • ok, did you change mods for am.setMode manager? ru.androids.help/q8521 - chall2
    • one
      Thanks for participating. The code that turned on the headphones and turned off the speakers: AudioManager am1 = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); am1.setMode(AudioManager.MODE_NORMAL); am1.setMode(AudioManager.MODE_IN_CALL); am1.setSpeakerphoneOn(false); AudioManager am1 = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); am1.setMode(AudioManager.MODE_NORMAL); am1.setMode(AudioManager.MODE_IN_CALL); am1.setSpeakerphoneOn(false); But I did not understand one thing: do I need to use such code in all the applications that I write myself? Automatically at the level of Andoid this does not happen? I think this is strange. And for headsets you need to add something like that? - Sergey
    • @ Sergey maybe write a response with your code? And then he is lost in the comments. - Nick Volynkin

    Colleagues, at the request of the moderator, publish the code that helped me. I do not pretend to be correct, because I know little about the topic. However:

      AudioManager am1 = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); am1.setMode(AudioManager.MODE_NORMAL); am1.setMode(AudioManager.MODE_IN_CALL); am1.setSpeakerphoneOn(false);