use opus for voice messages on Android 10

recipients need at least Android 5 to play the message; however
Conversations has been Android 5+ for a while now
This commit is contained in:
Daniel Gultsch 2023-08-20 07:59:39 +02:00
parent 6323b14bc0
commit 1b05cbd665
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -98,11 +98,20 @@ public class RecordingActivity extends Activity implements View.OnClickListener
private boolean startRecording() { private boolean startRecording() {
mRecorder = new MediaRecorder(); mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); final int outputFormat;
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
mRecorder.setAudioEncodingBitRate(96000); outputFormat = MediaRecorder.OutputFormat.OGG;
mRecorder.setAudioSamplingRate(22050); mRecorder.setOutputFormat(outputFormat);
setupOutputFile(); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.OPUS);
mRecorder.setAudioEncodingBitRate(32000);
} else {
outputFormat = MediaRecorder.OutputFormat.MPEG_4;
mRecorder.setOutputFormat(outputFormat);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioEncodingBitRate(96000);
mRecorder.setAudioSamplingRate(22050);
}
setupOutputFile(outputFormat);
mRecorder.setOutputFile(mOutputFile.getAbsolutePath()); mRecorder.setOutputFile(mOutputFile.getAbsolutePath());
try { try {
@ -110,10 +119,10 @@ public class RecordingActivity extends Activity implements View.OnClickListener
mRecorder.start(); mRecorder.start();
mStartTime = SystemClock.elapsedRealtime(); mStartTime = SystemClock.elapsedRealtime();
mHandler.postDelayed(mTickExecutor, 100); mHandler.postDelayed(mTickExecutor, 100);
Log.d("Voice Recorder", "started recording to " + mOutputFile.getAbsolutePath()); Log.d(Config.LOGTAG, "started recording to " + mOutputFile.getAbsolutePath());
return true; return true;
} catch (Exception e) { } catch (Exception e) {
Log.e("Voice Recorder", "prepare() failed " + e.getMessage()); Log.e(Config.LOGTAG, "prepare() failed ", e);
return false; return false;
} }
} }
@ -175,9 +184,18 @@ public class RecordingActivity extends Activity implements View.OnClickListener
} }
} }
private File generateOutputFilename() { private File generateOutputFilename(final int outputFormat) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US); final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
final String filename = "RECORDING_" + dateFormat.format(new Date()) + ".m4a"; final String extension;
if (outputFormat == MediaRecorder.OutputFormat.MPEG_4) {
extension = "m4a";
} else if (outputFormat == MediaRecorder.OutputFormat.OGG) {
extension = "oga";
} else {
throw new IllegalStateException("Unrecognized output format");
}
final String filename =
String.format("RECORDING_%s.%s", dateFormat.format(new Date()), extension);
final File parentDirectory; final File parentDirectory;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
parentDirectory = parentDirectory =
@ -190,8 +208,8 @@ public class RecordingActivity extends Activity implements View.OnClickListener
return new File(conversationsDirectory, filename); return new File(conversationsDirectory, filename);
} }
private void setupOutputFile() { private void setupOutputFile(final int outputFormat) {
mOutputFile = generateOutputFilename(); mOutputFile = generateOutputFilename(outputFormat);
final File parentDirectory = mOutputFile.getParentFile(); final File parentDirectory = mOutputFile.getParentFile();
if (Objects.requireNonNull(parentDirectory).mkdirs()) { if (Objects.requireNonNull(parentDirectory).mkdirs()) {
Log.d(Config.LOGTAG, "created " + parentDirectory.getAbsolutePath()); Log.d(Config.LOGTAG, "created " + parentDirectory.getAbsolutePath());