Android - Play audio from earpiece

后端 未结 7 1983
旧巷少年郎
旧巷少年郎 2020-12-02 08:27

I\'d like to play audio from a file into the phone\'s earpiece (or headset of that is connected) using the android MediaPlayer. I tried using the MODE_IN_CALL h

7条回答
  •  [愿得一人]
    2020-12-02 09:06

    It turns out the right way to do this is through the following code.

    Play through the ear piece

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    

    Play through the speaker phone

    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    

    That is it. Any other solution (Including the one posted here -> Android - Getting audio to play through earpiece) does not work consistently across devices. In fact using MODE_IN_CALL ensures that your audio will never play on certain devices.

    Note: You will need to re-prepare the media player to change streams.

提交回复
热议问题