Skip to content

Commit

Permalink
Recording audio with AAC in MPEG-4 (M4A/MP4; #729)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Mar 29, 2020
1 parent 94e9e7e commit aca572e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/org/kontalk/ui/AudioDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/java/org/kontalk/ui/view/ComposerBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/kontalk/util/AudioRecording.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/kontalk/util/MediaStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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;
}
Expand Down

0 comments on commit aca572e

Please sign in to comment.