Skip to content

Bluetooth headphone usage

Overview

Since build 1.0.1.74 it is possible to swith to Bluetooth headset for audio input/output using Flashphoner.getAudioManager().setUseBluetoothSco() function call:

To switch to BT headset

Flashphoner.getAudioManager().setUseBluetoothSco(true);

To switch to other audio input/output device

Flashphoner.getAudioManager().setUseBluetoothSco(false);

The usage example. Please note that it is necessary to disable speakerphone and BT headset usage to switch to phone voice speaker or to wired headset.

code

mAudioOutput = (LabelledSpinner) findViewById(R.id.audio_output);
mAudioOutput.setOnItemChosenListener(new LabelledSpinner.OnItemChosenListener() {
    @Override
    public void onItemChosen(View labelledSpinner, AdapterView<?> adapterView, View itemView, int position, long id) {
        String audioType = getResources().getStringArray(R.array.audio_output)[position];
        switch (audioType) {
            case "speakerphone": Flashphoner.getAudioManager().setUseSpeakerPhone(true); break;
            case "phone":
                Flashphoner.getAudioManager().setUseBluetoothSco(false);
                Flashphoner.getAudioManager().setUseSpeakerPhone(false);
                break;
            case "bluetooth": Flashphoner.getAudioManager().setUseBluetoothSco(true); break;
        }
    }

    @Override
    public void onNothingChosen(View labelledSpinner, AdapterView<?> adapterView) {

    }
});

A simultaneous playback of media stream and a local media file to BT headphone

It is necessary to set the attribute AudioAttributes.USAGE_VOICE_COMMUNICATION and request audio focus for a mediaplayer in application to play simultaneously a media stream from server and a local media file from device to Bluetooth headphone, for example

/**
    * Audio focus example
    */
MediaPlayer music = MediaPlayer.create(getBaseContext(), R.raw.sound1);
music.setAudioAttributes(
        new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
            .build());
music.start();
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
    @Override
    public void onAudioFocusChange(int focusChange) {
        Log.d("AudioFocus,", "onAudioFocusChange="+focusChange);
        music.setVolume(1, 1);
    }
};
// Request audio focus for playback
int requestResult = Flashphoner.getAudioManager().getAudioManager().requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
Log.d("AudioFocus,", "requestResult="+requestResult);
// Abandon audio focus when playback complete
music.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        Flashphoner.getAudioManager().getAudioManager().abandonAudioFocus(audioFocusChangeListener);
    }
});