0

I'm making an application that records audio through a Bluetooth headset(or the phone's mic if there isn't a headset connected).

The application is intended to run in the background, while the user may be using the phone for something else, so it runs within a foreground service that is started manually by the user. While the device is recording, any audio that is playing(e.g. YouTube video or music from an app that also runs in the background) has a much lower playback volume than normal. This only happens when recording through the Bluetooth headset, while with the phone's default mic there isn't any effect.

The code I am using for the recorder part is the one described in the question at this post: https://stackoverflow.com/questions/4026002/how-to-record-sound-using-bluetooth-headset and the one for establishing the connection with the headset is the one from Stephan's answer from the same thread.

Edit: Per suggestion the code samples that implement the recording are these:

In main activity:

@Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
        registerReceiver(mBluetoothScoReceiver, intentFilter);
        audioManager = (AudioManager) getApplicationContext().getSystemService(getApplicationContext().AUDIO_SERVICE);
        // Start Bluetooth SCO.
        audioManager.setMode(audioManager.MODE_NORMAL);
        audioManager.setBluetoothScoOn(true);
        audioManager.startBluetoothSco();
        // Stop Speaker.
        audioManager.setSpeakerphoneOn(false);

    }

@Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBluetoothScoReceiver);
        // Stop Bluetooth SCO.
        audioManager.stopBluetoothSco();
        audioManager.setMode(audioManager.MODE_NORMAL);
        audioManager.setBluetoothScoOn(false);
        // Start Speaker.
        audioManager.setSpeakerphoneOn(true);
    }

private BroadcastReceiver mBluetoothScoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);
            System.out.println("ANDROID Audio SCO state: " + state);
            if (AudioManager.SCO_AUDIO_STATE_CONNECTED == state) {
                /*
                 * Now the connection has been established to the bluetooth device.
                 * Record audio or whatever (on another thread).With AudioRecord you can record with an object created like this:
                 * new AudioRecord(MediaRecorder.AudioSource.MIC, 8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                 * AudioFormat.ENCODING_PCM_16BIT, audioBufferSize);
                 *
                 * After finishing, don't forget to unregister this receiver and
                 * to stop the bluetooth connection with am.stopBluetoothSco();
                 */
            }
        }
    };

In the Audiomanager class (responsible for the recording):

private MediaRecorder recorder;

    /**
     * Class constructor, creates the recorder
     */
    public AudioCaptureManager() {
        recorder = new MediaRecorder();
    }

    /**
     * Creates the audio file, sets the recorders parameters and starts the recording
     */
    public void start() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
        String externalFilesDir = Environment.getExternalStoragePublicDirectory("/").getAbsolutePath();/*
        We can change this directory to whatever we want*/
        String file = sdf.format(new Date()) + ".aac";
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
        recorder.setOutputFile(externalFilesDir + "/Recordings/" + file);
        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        recorder.start();
    }

    /**
     * Stops the audio recording and clears the recorder
     */
    public void stop() {
        recorder.stop();
        recorder.release();
        recorder = null;
    }

The audiomanager's methods are called on demand by buttons in my UI.

So, is there any way to fix/avoid this?

0 Answers0