From aca572e76b9b8e4a2f23f7fc9b8b73af9d5b037c Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Sun, 29 Mar 2020 18:55:47 +0200 Subject: [PATCH] Recording audio with AAC in MPEG-4 (M4A/MP4; #729) Signed-off-by: Daniele Ricci --- .../java/org/kontalk/ui/AbstractComposeFragment.java | 3 ++- app/src/main/java/org/kontalk/ui/AudioDialog.java | 5 ----- .../main/java/org/kontalk/ui/view/ComposerBar.java | 5 +---- app/src/main/java/org/kontalk/util/AudioRecording.kt | 11 +++++++++-- app/src/main/java/org/kontalk/util/MediaStorage.java | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/org/kontalk/ui/AbstractComposeFragment.java b/app/src/main/java/org/kontalk/ui/AbstractComposeFragment.java index 848bb578a..208965633 100644 --- a/app/src/main/java/org/kontalk/ui/AbstractComposeFragment.java +++ b/app/src/main/java/org/kontalk/ui/AbstractComposeFragment.java @@ -128,6 +128,7 @@ import org.kontalk.ui.view.ComposerListener; import org.kontalk.ui.view.MessageListItem; import org.kontalk.ui.view.ReplyBar; +import org.kontalk.util.AudioRecording; import org.kontalk.util.MediaStorage; import org.kontalk.util.MessageUtils; import org.kontalk.util.Permissions; @@ -2419,7 +2420,7 @@ public void textChanged(CharSequence text) { @Override public void onRecordingSuccessful(File file) { if (file != null) - sendBinaryMessage(Uri.fromFile(file), AudioDialog.DEFAULT_MIME, false, AudioComponent.class); + sendBinaryMessage(Uri.fromFile(file), AudioRecording.MIME_TYPE, false, AudioComponent.class); } @Override diff --git a/app/src/main/java/org/kontalk/ui/AudioDialog.java b/app/src/main/java/org/kontalk/ui/AudioDialog.java index 3b8d2c143..e9706754c 100644 --- a/app/src/main/java/org/kontalk/ui/AudioDialog.java +++ b/app/src/main/java/org/kontalk/ui/AudioDialog.java @@ -67,8 +67,6 @@ public class AudioDialog extends AlertDialog { private static final String STATE_PREFIX = "AudioDialog_"; - public static final String DEFAULT_MIME = "audio/3gpp"; - private static final int STATUS_IDLE = 0; private static final int STATUS_RECORDING = 1; private static final int STATUS_STOPPED = 2; @@ -375,9 +373,6 @@ void startRecord() throws IOException { mData.startRecording(); mStatus = STATUS_RECORDING; } - catch (IllegalStateException e) { - Log.e(TAG, "error starting audio recording", e); - } catch (IOException e) { Log.e(TAG, "error writing on external storage", e); cancel(); diff --git a/app/src/main/java/org/kontalk/ui/view/ComposerBar.java b/app/src/main/java/org/kontalk/ui/view/ComposerBar.java index afefb2236..5dd001462 100644 --- a/app/src/main/java/org/kontalk/ui/view/ComposerBar.java +++ b/app/src/main/java/org/kontalk/ui/view/ComposerBar.java @@ -573,9 +573,6 @@ private void doStartRecording() { lockScreen(); disableTextEntry(); } - catch (IllegalStateException e) { - Log.e(TAG, "error starting audio recording:", e); - } catch (IOException e) { Log.e(TAG, "error writing on external storage:", e); Toast.makeText(mContext, R.string.err_audio_record_writing, @@ -615,7 +612,7 @@ void stopRecording(boolean send) { if (canSend) { if (mListener != null) { mListener.sendBinaryMessage(Uri.fromFile(mRecordFile), - AudioDialog.DEFAULT_MIME, false, AudioComponent.class); + AudioRecording.MIME_TYPE, false, AudioComponent.class); } } else if (send) { diff --git a/app/src/main/java/org/kontalk/util/AudioRecording.kt b/app/src/main/java/org/kontalk/util/AudioRecording.kt index bd3ee597a..5b99deff0 100644 --- a/app/src/main/java/org/kontalk/util/AudioRecording.kt +++ b/app/src/main/java/org/kontalk/util/AudioRecording.kt @@ -29,13 +29,20 @@ import java.io.File class AudioRecording { companion object { + /** TODO change to m4a after some time (this is for backward compatibility) */ + const val MIME_TYPE = "audio/mp4" + const val FILE_EXTENSION = "mp4"; @JvmStatic fun setupMediaRecorder(recorder: MediaRecorder, outputFile: File): MediaRecorder { recorder.setAudioSource(MediaRecorder.AudioSource.MIC); - recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); + recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); recorder.setOutputFile(outputFile.absolutePath); - recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); + recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); + recorder.setAudioChannels(1); + // TODO we need further study for these + //recorder.setAudioSamplingRate(48000); + //recorder.setAudioEncodingBitRate(96000); return recorder; } diff --git a/app/src/main/java/org/kontalk/util/MediaStorage.java b/app/src/main/java/org/kontalk/util/MediaStorage.java index c13e73cab..66cafe528 100644 --- a/app/src/main/java/org/kontalk/util/MediaStorage.java +++ b/app/src/main/java/org/kontalk/util/MediaStorage.java @@ -446,7 +446,7 @@ public static File getIncomingImageFile(Date date, String extension) { return new File(PICTURES_ROOT, "IMG_" + timeStamp + "." + extension); } - /** Creates a temporary 3gp file. */ + /** Creates a temporary audio file. */ public static File getOutgoingAudioFile(Context context) throws IOException { return getOutgoingAudioFile(context, new Date()); } @@ -455,7 +455,7 @@ private static File getOutgoingAudioFile(Context context, Date date) throws IOEx File path = new File(context.getExternalFilesDir(RECORDING_ROOT_TYPE), RECORDING_SENT_ROOT); createNoMedia(path); String timeStamp = sDateFormat.format(date); - File f = new File(path, "record_" + timeStamp + ".3gp"); + File f = new File(path, "record_" + timeStamp + "." + AudioRecording.FILE_EXTENSION); f.createNewFile(); return f; }