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

Feature/current metering android #296

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class AudioRecorderManager extends ReactContextBaseJavaModule {
private boolean isPauseResumeCapable = false;
private Method pauseMethod = null;
private Method resumeMethod = null;
private int progressUpdateInterval = 1000;


public AudioRecorderManager(ReactApplicationContext reactContext) {
Expand Down Expand Up @@ -126,6 +127,7 @@ public void prepareRecordingAtPath(String recordingPath, ReadableMap recordingSe
recorder.setAudioEncodingBitRate(recordingSettings.getInt("AudioEncodingBitRate"));
recorder.setOutputFile(recordingPath);
includeBase64 = recordingSettings.getBoolean("IncludeBase64");
setProgressUpdateInterval(recordingSettings.getInt("ProgressUpdateInterval"));
}
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());
Expand Down Expand Up @@ -312,10 +314,18 @@ public void run() {
if (!isPaused) {
WritableMap body = Arguments.createMap();
body.putDouble("currentTime", stopWatch.getTimeSeconds());

int amplitude = recorder.getMaxAmplitude();
if (amplitude == 0) {
body.putInt("currentMetering", -160);
} else {
body.putInt("currentMetering", (int) (20 * Math.log(((double) amplitude) / 32767d)));
}

sendEvent("recordingProgress", body);
}
}
}, 0, 1000);
}, 0, progressUpdateInterval);
}

private void stopTimer(){
Expand All @@ -336,4 +346,13 @@ private void logAndRejectPromise(Promise promise, String errorCode, String error
Log.e(TAG, errorMessage);
promise.reject(errorCode, errorMessage);
}


private void setProgressUpdateInterval(int progressUpdateInterval) {
if(progressUpdateInterval < 100) {
this.progressUpdateInterval = 100;
} else {
this.progressUpdateInterval = progressUpdateInterval;
}
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var AudioRecorder = {
MeteringEnabled: false,
MeasurementMode: false,
AudioEncodingBitRate: 32000,
IncludeBase64: false
IncludeBase64: false,
ProgressUpdateInterval: 1000,

Choose a reason for hiding this comment

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

FYI, for iOS to work you also need to update line 60 below to add recordingOptions.ProgressUpdateInterval to the prepareRecordingAtPath call.

};

var recordingOptions = {...defaultOptions, ...options};
Expand Down
9 changes: 7 additions & 2 deletions ios/AudioRecorderManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ - (void)stopProgressTimer {
}

- (void)startProgressTimer {
_progressUpdateInterval = 250;
//_prevProgressUpdateTime = nil;

[self stopProgressTimer];
Expand Down Expand Up @@ -121,7 +120,7 @@ - (NSString *) applicationDocumentsDirectory
return basePath;
}

RCT_EXPORT_METHOD(prepareRecordingAtPath:(NSString *)path sampleRate:(float)sampleRate channels:(nonnull NSNumber *)channels quality:(NSString *)quality encoding:(NSString *)encoding meteringEnabled:(BOOL)meteringEnabled measurementMode:(BOOL)measurementMode includeBase64:(BOOL)includeBase64)
RCT_EXPORT_METHOD(prepareRecordingAtPath:(NSString *)path sampleRate:(float)sampleRate channels:(nonnull NSNumber *)channels quality:(NSString *)quality encoding:(NSString *)encoding meteringEnabled:(BOOL)meteringEnabled measurementMode:(BOOL)measurementMode includeBase64:(BOOL)includeBase64 progressUpdateInterval:(int)progressUpdateInterval)
{
_prevProgressUpdateTime = nil;
[self stopProgressTimer];
Expand All @@ -135,6 +134,12 @@ - (NSString *) applicationDocumentsDirectory
_audioSampleRate = [NSNumber numberWithFloat:44100.0];
_meteringEnabled = NO;
_includeBase64 = NO;
_progressUpdateInterval = 250;

// Set default progressUpdateInterval
if (progressUpdateInterval != nil) {
_progressUpdateInterval = progressUpdateInterval;
}

// Set audio quality from options
if (quality != nil) {
Expand Down