Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve audio recording configuration #2752

Merged
merged 14 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/assets/locales/android_translatable_strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,12 @@ custom.restore.file.not.set=Custom restore file path is not set in preferences
custom.restore.error=Error loading custom sync

start.recording=Start Recording
start.recording.failed=Unable to start recording while another application is recording!
stop.recording=Stop Recording
recording.header=Record a sound
before.recording=Tap to start recording
before.overwrite.recording=Tap to start a new recording
during.recording=Tap to stop recording
during.recording=Recording is in progress, avoid navigating away from CommCare. Tap to stop recording
after.recording=Recording complete.
delete.recording=Tap to delete recording
pause.recording=Recording paused, tap to continue recording
Expand Down
11 changes: 9 additions & 2 deletions app/src/org/commcare/utils/MediaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Pair;
import android.view.WindowManager;

import org.commcare.CommCareApplication;
import org.commcare.engine.references.JavaFileReference;
import org.commcare.google.services.analytics.AnalyticsParamValue;
import org.commcare.google.services.analytics.FirebaseAnalyticsUtil;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.util.LogTypes;
import org.javarosa.core.reference.InvalidReferenceException;
Expand All @@ -27,6 +27,7 @@
import java.security.NoSuchAlgorithmException;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

/**
* @author ctsims
Expand Down Expand Up @@ -506,4 +507,10 @@ private static Pair<Bitmap, Boolean> performSafeScaleDown(String imageFilepath,
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
public static boolean isRecordingActive(Context context){
return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE))
.getActiveRecordingConfigurations().size() > 0;
}

}
20 changes: 18 additions & 2 deletions app/src/org/commcare/views/widgets/RecordingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import org.commcare.CommCareApplication;
import org.commcare.dalvik.R;
import org.commcare.utils.MediaUtil;
import org.javarosa.core.services.locale.Localization;

import java.io.File;
Expand Down Expand Up @@ -165,6 +167,13 @@ private void resetRecordingView() {
}

private void startRecording() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (MediaUtil.isRecordingActive(getContext())) {
Toast.makeText(getContext(), Localization.get("start.recording.failed"), Toast.LENGTH_SHORT).show();
return;
}
}

disableScreenRotation((AppCompatActivity)getContext());
setCancelable(false);
setupRecorder();
Expand Down Expand Up @@ -196,8 +205,15 @@ private void setupRecorder() {
}

boolean isHeAacSupported = isHeAacEncoderSupported();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
} else {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test that the recording with source set to VOICE_COMMUNICATION are playable in browser (instead of download) on HQ.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I tested it and it plays normally.


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
recorder.setPrivacySensitive(true);
}
recorder.setAudioSamplingRate(isHeAacSupported ? HEAAC_SAMPLE_RATE : AMRNB_SAMPLE_RATE);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(fileName);
Expand Down
Loading