@mgcrea/react-native-tone-generator
v0.4.1
Published
React Native plugin exposing the Android ToneGenerator and AudioManager stream volume
Maintainers
Readme
@mgcrea/react-native-tone-generator
React Native plugin exposing Android's ToneGenerator
(DTMF, call supervisory and proprietary tones) and AudioManager stream volume.
- Android only. Both are Android platform APIs; autolinking skips iOS automatically, so you do not need to exclude this package by hand.
- Requires React Native >= 0.86 (New Architecture / TurboModule).
Installation
npm install @mgcrea/react-native-tone-generatorThen rebuild the Android app. No pod install step is needed — the package declares no iOS
platform.
Usage
import {
startTone,
startToneAndWait,
stopTone,
getStreamMaxVolume,
setStreamVolume,
TONE_PROP_BEEP,
STREAM_MUSIC,
} from '@mgcrea/react-native-tone-generator';
// Play a beep for 400ms.
await startTone(TONE_PROP_BEEP, 400);
// Play it and wait for it to finish.
await startToneAndWait(TONE_PROP_BEEP, 400);
// Volume.
const max = await getStreamMaxVolume(STREAM_MUSIC);
await setStreamVolume(Math.floor(max / 2), STREAM_MUSIC);Tones
| Function | Description |
| --- | --- |
| startTone(toneType, durationMs = -1) | Starts a tone. -1 plays until stopTone(). |
| startToneAndWait(toneType, durationMs = -1) | Starts a tone and waits out durationMs. |
| stopTone() | Stops the current tone. |
| configureTone(streamType, volume = 100) | Rebuilds the generator on a stream at a volume (0–100). |
Volume
| Function | Description |
| --- | --- |
| getStreamVolume(streamType = STREAM_MUSIC) | Current volume for the stream. |
| setStreamVolume(index, streamType = STREAM_MUSIC, flags = 0) | Sets the stream volume. |
| getStreamMaxVolume(streamType = STREAM_MUSIC) | Maximum volume for the stream. |
| getStreamMinVolume(streamType = STREAM_MUSIC) | Minimum volume, or null below Android 9 (API 28). |
Note setStreamVolume takes the index first and the stream second.
Constants
All constants are plain JavaScript, so they are safe to use at module scope:
- Tones:
TONE_DTMF_0…TONE_DTMF_D,TONE_SUP_*,TONE_PROP_*,TONE_CDMA_*. - Streams:
STREAM_VOICE_CALL,STREAM_SYSTEM,STREAM_RING,STREAM_MUSIC,STREAM_ALARM,STREAM_NOTIFICATION,STREAM_BLUETOOTH_SCO,STREAM_SYSTEM_ENFORCED,STREAM_DTMF,STREAM_TTS,STREAM_ACCESSIBILITY. - Volume bounds for
configureTone:MIN_VOLUME(0),MAX_VOLUME(100).
Compatibility
| Package | React Native |
| --- | --- |
| >= 0.4.0 | >= 0.86 |
| <= 0.3.1 | 0.70 (Old Architecture) |
Development
pnpm install # install the library's dependencies
pnpm run build # compile src/ -> dist/
pnpm test # lint + typecheck + unit tests
cd example
pnpm install
pnpm run android # build and run the example on a deviceThe example app links the library via "@mgcrea/react-native-tone-generator": "link:..", so
autolinking picks it up from node_modules and Metro resolves it through
@mgcrea/metro-plugin-linked-packages.
License
MIT
