punarinta / react-native-sound-level

A package to dynamically measure sound input level in React Native applications. Can be used to help user to adjust microphone sensitivity.
MIT License
79 stars 45 forks source link

Missing `return` and error handling #34

Open gavrilikhin-d opened 1 year ago

gavrilikhin-d commented 1 year ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch react-native-sound-level@1.2.0 for the project I'm working on.

You seems to forget return from function when MediaRecorder can't prepare. This, probably, leads to a crash in recorder.start().

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-sound-level/android/src/main/java/com/punarinta/RNSoundLevel/RNSoundLevelModule.java b/node_modules/react-native-sound-level/android/src/main/java/com/punarinta/RNSoundLevel/RNSoundLevelModule.java
index 35b3ac4..d788044 100644
--- a/node_modules/react-native-sound-level/android/src/main/java/com/punarinta/RNSoundLevel/RNSoundLevelModule.java
+++ b/node_modules/react-native-sound-level/android/src/main/java/com/punarinta/RNSoundLevel/RNSoundLevelModule.java
@@ -63,9 +63,15 @@ class RNSoundLevelModule extends ReactContextBaseJavaModule {
       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;

This issue body was partially generated by patch-package.