Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on start #35

Merged
merged 4 commits into from
Dec 17, 2024
Merged
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 @@ -63,9 +63,15 @@ public void start(int monitorInterval, int samplingRate, Promise promise) {
recorder.prepare();
} catch (final Exception e) {
logAndRejectPromise(promise, "COULDNT_PREPARE_RECORDING", e.getMessage());
return;
}

recorder.start();
try {
recorder.start();
} catch (final Exception e) {
logAndRejectPromise(promise, "COULDNT_START_RECORDING", e.getMessage());
return;
}

frameId = 0;
isRecording = true;
Expand Down Expand Up @@ -136,7 +142,7 @@ private void sendEvent(String eventName, Object params) {
}

private void logAndRejectPromise(Promise promise, String errorCode, String errorMessage) {
Log.e(TAG, errorMessage);
Log.e(TAG, "[" + errorCode + "] " + errorMessage);
promise.reject(errorCode, errorMessage);
}
}
37 changes: 30 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
export type SoundLevelResult = {
/**
* @description Frame number
* Frame number
*/
id: number;

/**
* @description Sound level in decibels
* @description -160 is a silence
* Sound level in decibels
*
* @note -160 is a silence
*/
value: number;

/**
* @description raw level value, OS-depended
* Raw level value, OS-depended
*/
rawValue: number;
}
Expand All @@ -23,10 +24,32 @@ export type SoundLevelMonitorConfig = {

export type SoundLevelType = {
/**
* @description monitoringInterval is not supported for desktop yet
* Start monitoring sound level
*
* @note `monitoringInterval` is not supported for desktop yet
*
* @note don't forget to call `stop` eventually
*
* on Android:
* @throws
* - `INVALID_STATE` - sound level recording already started
* - `COULDNT_CONFIGURE_MEDIA_RECORDER` - recording configuration failed (probably due-to lack of android.permission.RECORD_AUDIO)
* - `COULDNT_PREPARE_RECORDING` - preparation fails for some reason
* - `COULDNT_START_RECORDING` - can't start recording session (another app is using recording?)
*/
start: (config?: number | SoundLevelMonitorConfig) => void;
stop: () => void;
start: (config?: number | SoundLevelMonitorConfig) => Promise<void>;
/**
* Stop monitoring sound level
*
* on Android:
* @throws
* - `INVALID_STATE` - sound level recording hasn't been started
* - `RUNTIME_EXCEPTION` - no valid audio data received. You may be using a device that can't record audio.
*/
stop: () => Promise<void>;
/**
* User-provided callback to call on each frame
*/
onNewFrame: (result: SoundLevelResult) => void;
}

Expand Down
2 changes: 1 addition & 1 deletion ios/RNSoundLevelModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (void)startProgressTimer:(int)monitorInterval {
[NSNumber numberWithInt:AVAudioQualityLow], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
[NSNumber numberWithInt:1], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:samplingRate], AVSampleRateKey,
[NSNumber numberWithFloat:sampleRate], AVSampleRateKey,
nil];

NSError *error = nil;
Expand Down