Skip to content

Commit

Permalink
Check if microphone is capturing audio on Android 9 and lower
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed May 6, 2024
1 parent 7f0942d commit 246f153
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions app/src/org/commcare/views/widgets/RecordingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private void setupRecorder() {
} else {
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
registerAudioRecordingConfigurationChangeCallback();
}
try {
Expand Down Expand Up @@ -426,7 +426,7 @@ private void chronoResume() {
recordingDuration.start();
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@RequiresApi(api = Build.VERSION_CODES.N)
private void registerAudioRecordingConfigurationChangeCallback() {
audioRecordingCallback = new AudioManager.AudioRecordingCallback() {
@Override
Expand Down Expand Up @@ -460,7 +460,7 @@ public void onRecordingConfigChanged(List<AudioRecordingConfiguration> configs)
.registerAudioRecordingCallback(audioRecordingCallback, null);
}

@RequiresApi(api = Build.VERSION_CODES.Q)
@RequiresApi(api = Build.VERSION_CODES.N)
private void unregisterAudioRecordingConfigurationChangeCallback() {
if (audioRecordingCallback != null) {
((AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE))
Expand All @@ -469,16 +469,26 @@ private void unregisterAudioRecordingConfigurationChangeCallback() {
}
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private boolean hasRecordingGoneSilent(List<AudioRecordingConfiguration> configs) {
if (recorder == null || recorder.getActiveRecordingConfiguration() == null) {
if (recorder == null) {
return false;
}

Optional<AudioRecordingConfiguration> currentAudioConfig =
configs.stream().filter(config -> config.getClientAudioSessionId()
== recorder.getActiveRecordingConfiguration().getClientAudioSessionId())
.findAny();
return currentAudioConfig.isPresent() ? currentAudioConfig.get().isClientSilenced() : false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
if (recorder.getActiveRecordingConfiguration() == null) {
return false;
}

Optional<AudioRecordingConfiguration> currentAudioConfig = configs.stream().filter(config ->
config.getClientAudioSessionId() == recorder.getActiveRecordingConfiguration()
.getClientAudioSessionId())
.findAny();
return currentAudioConfig.isPresent() ? currentAudioConfig.get().isClientSilenced() : false;
} else {
if (recorder.getMaxAmplitude() == 0) {
return true;
}
return false;
}
}
}

0 comments on commit 246f153

Please sign in to comment.