0

We call the startRecord method from the Firebase JobService. When our application is in the foreground, sound recording from the microphone passes without problems. However, if the application is in the background (no foreground activity), the MediaRecorder records silence. The recording also happens without problems if you call startRecord from the foreground service, however, our application must make a hidden recording. Here is the startRecord method:

void startRecord(int duration) {
    audioFile = Environment.getExternalStorageDirectory() + "/record.amr";

    try {
        File outFile = new File(audioFile);
        if (outFile.exists()) {
            outFile.delete();
        }

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setOutputFile(audioFile);
        mediaRecorder.prepare();
        mediaRecorder.start();

        //start timer for stop recording
        handler.postDelayed(StopRecordTask, duration * 1000);

        Log.d(LOG_TAG, "Audio record start");
    } catch (Exception e) {
        if (mediaRecorder != null) {
            mediaRecorder.release();
            mediaRecorder = null;
        }
        errorMessage();
        e.printStackTrace();
    }
}

We use android SDK version 26. Tell me, please, what is the cause of the problem?

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
SolderingIronMen
  • 554
  • 1
  • 5
  • 20

0 Answers0