Skip to main content

Usage

API

Below you can see small code snippet that shows how to import and use the main functions.

import { PitchDetector } from 'react-native-pitch-detector';

// To start recording
await PitchDetector.start(); // Promise<void>

// To stop recording
await PitchDetector.stop(); // Promise<void>

// To get current status
await PitchDetector.isRecording(); // Promise<true | false>

// To listener results
const subscription = PitchDetector.addListener(console.log) // { frequency: 440.14782, tone: "C#" }

// To stop listen results
PitchDetector.removeListener()

You can see a complete implementation at Playground.

Permissions

To use microphone we need give permission to our app, for that we use a react-native-permissions library.

 yarn add react-native-permissions@3

Or using npm:

npm install react-native-permissions@3

After that, on package.json file add de following lines:

"reactNativePermissionsIOS": [
"Microphone"
]

For iOS you need edit Info.plist, add:

<key>NSMicrophoneUsageDescription</key>
<string>Perform pitch detection</string>

Lastly for iOS you need execute:

(cd ios && pod install)

For Android you need change AndroidManifest.xml, and add:

<uses-permission android:name="android.permission.RECORD_AUDIO" />

and finally recompile your project both platforms.

💡 In future the idea is remove react-native-permission dependency and develop in house permission manager.