Skip to content
This repository was archived by the owner on Aug 21, 2020. It is now read-only.

add recording for duration #312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions android/src/main/java/com/rnim/rn/audio/AudioRecorderManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,44 @@ public void prepareRecordingAtPath(String recordingPath, ReadableMap recordingSe

}

@ReactMethod
public void prepareRecordingForDuration(String recordingPath, ReadableMap recordingSettings, int duration, Promise promise) {
if (isRecording){
logAndRejectPromise(promise, "INVALID_STATE", "Please call stopRecording before starting recording");
}
File destFile = new File(recordingPath);
if (destFile.getParentFile() != null) {
destFile.getParentFile().mkdirs();
}
recorder = new MediaRecorder();
try {
recorder.setAudioSource(recordingSettings.getInt("AudioSource"));
int outputFormat = getOutputFormatFromString(recordingSettings.getString("OutputFormat"));
recorder.setOutputFormat(outputFormat);
recorder.setMaxDuration(duration*1000);
int audioEncoder = getAudioEncoderFromString(recordingSettings.getString("AudioEncoding"));
recorder.setAudioEncoder(audioEncoder);
recorder.setAudioSamplingRate(recordingSettings.getInt("SampleRate"));
recorder.setAudioChannels(recordingSettings.getInt("Channels"));
recorder.setAudioEncodingBitRate(recordingSettings.getInt("AudioEncodingBitRate"));
recorder.setOutputFile(destFile.getPath());
includeBase64 = recordingSettings.getBoolean("IncludeBase64");
}
catch(final Exception e) {
logAndRejectPromise(promise, "COULDNT_CONFIGURE_MEDIA_RECORDER" , "Make sure you've added RECORD_AUDIO permission to your AndroidManifest.xml file "+e.getMessage());
return;
}

currentOutputFile = recordingPath;
try {
recorder.prepare();
promise.resolve(currentOutputFile);
} catch (final Exception e) {
logAndRejectPromise(promise, "COULDNT_PREPARE_RECORDING_AT_PATH "+recordingPath, e.getMessage());
}

}

private int getAudioEncoderFromString(String audioEncoder) {
switch (audioEncoder) {
case "aac":
Expand Down
7 changes: 7 additions & 0 deletions ios/AudioRecorderManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ - (NSString *) applicationDocumentsDirectory
[_audioRecorder record];
}

RCT_EXPORT_METHOD(startRecordForDuration: (NSTimeInterval)time)
{
[self startProgressTimer];
[_recordSession setActive:YES error:nil];
[_audioRecorder recordForDuration: time];
}

RCT_EXPORT_METHOD(stopRecording)
{
[_audioRecorder stop];
Expand Down