Томас Петр Томас Петр - 1 month ago 16
Android Question

Android: MediaPlayer - handsfree volume

I'm trying to manually disable / enable playback of the audio to a Bluetooth device (handsfree in the car). So I need MediaPlayer set to be played from speakers on the phone ONLY or a speaker Bluetooth device ONLY - BUT of course when Bluetooth device are connected - if not, I want to use phone speaker.

I managed to do that only in this way:

AudioManager ax = (AudioManager) getSystemService(AUDIO_SERVICE);
player.setAudioStreamType(AudioManager.MODE_NORMAL);
ax.setMode(AudioManager.MODE_NORMAL);


This works great but just when I use this line

ax.setSpeakerphoneOn(true);


When i'm using "false" on this line, the speaker is quietly and I don't want that (but everything else works as it should, just the sound is quietly).

so I want a way to :


  1. play sound from the speakers in the phone (only)

  2. play sound from the bluetooth device (only)

  3. when device is not connected - step 1.



If I'm using different modes, it does not work properly - eg. The sound plays int bluetooth device, but also phone speaker.

Thanks for answers.

Answer

OPA!

if(ONLY_PHONE_SPEAKER)
{
   ax.setStreamVolume(AudioManager.MODE_NORMAL, 20, 0);
   player.setAudioStreamType(AudioManager.MODE_NORMAL);
   ax.setSpeakerphoneOn(true);
}
else
{
    // BT enabled - only BT when is connected
    // speaker - when is not connected
    ax.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    ax.setSpeakerphoneOn(true);
}