mobile-rn-sound-recorder
A helper module to record voice audio in Redux. Redux/Saga wrapper for react-native-audio component.
Version 0.0.4, 2018/09/14
Module Public Interfaces
Constants
import { constants as soundRecorderConstants } from 'mobile-rn-sound-recorder'
General:
NAME - component name (for reducer)
Pre-defined pathes
PATH_BUNDLE - main bundle path
PATH_DOCUMENT - document path
PATH_DATA - path for data (inter-session) files
PATH_TEMP - path for temporary (session) files
Notification actions:
ON_ERROR - action invoked on processing error
ON_RECORDING_SAVED - action invoked just after sound file was successfully saved
Error codes:
ERROR_NOT_PERMITTED - recording audio is not permitted for application
ERROR_NOT_MOUNTED - component was not properly initialized (call mountRequest first)
ERROR_RECORDING - generic recording error (e.g. format not supported and so on)
ERROR_ALREADY_RUNNING - recording already running (on startRecording)
ERROR_FS - file system error
Action Creators
import { actions as soundRecorderActions } from 'mobile-rn-sound-recorder'
Init/Shut
/**
* Initializes component (should be called first)
* @param options.audioSettings audio settings (optinal, see iosAudioSettings, androidAudioSettings constants for details)
* @param options.lang language 'eng' or 'ger' (optinal, by default 'eng')
* @param options.wordsMap words Map(key, value) (optinal, used for PermissionsAndroid.request)
* @param options.logLevel logging level (0 - no debug info, default; 1; 2 - wordy log)
*/
const mountRequest = (options) => ({ type: constants.MOUNT_REQUEST, options });
/**
* Shutdowns component
*/
const unmountRequest = () => ({ type: constants.UNMOUNT_REQUEST });
Start/Stop/Save to file
/**
* Starts recording
*/
const startRequest = () => ({ type: constants.START_REQUEST });
/**
* Ends recording
* @param success false if stopped by error, true otherwise (true by default)
*/
const stopRequest = (success = true) => ({ type: constants.STOP_REQUEST, success });
/**
* Saves last recording to the file
* @param fileInfo.name file name w/o extension (optinal, if not specified generated by default)
* @param fileInfo.path file path (optinal, by default DocumentPath)
* @param userData user data object (optinal, passed as a payload with onFileSaved action)
*/
const saveAsFileRequest = (fileInfo, userData) => ({
type: constants.SAVE_AS_FILE_REQUEST,
fileInfo,
userData
});
Selectors
import { selectors as soundRecorderSelectors } from 'mobile-rn-sound-recorder'
soundRecorderSelectors.hasPermission() - true if the application acquired microphone permission
soundRecorderSelectors.getAudioSettings() - current audio settings *
soundRecorderSelectors.isMounted() - true if component is ready for recording (mounted)
soundRecorderSelectors.isRecording() - true if recording is in progress (between start & stop requests)
soundRecorderSelectors.isReadyToSave() - true if the last recording finished successfully and is ready
to be saved
soundRecorderSelectors.getCurrentTime() - current recording time in secs [Real Number]
soundRecorderSelectors.getInfo() - descriptor of the last successfully saved file **
soundRecorderSelectors.getError() - last error ***
// * Settings object:
// default settings for iOS
const iosAudioSettings = {
SampleRate: 22050,
Channels: 1,
AudioQuality: 'Low',
AudioEncoding: 'caf',
OutputFormat: 'mp3',
AudioEncodingBitRate: 32000
};
// default settings for Android
const androidAudioSettings = {
SampleRate: 22050,
Channels: 1,
AudioQuality: 'Low',
AudioEncoding: 'mp3',
OutputFormat: 'mp3',
AudioEncodingBitRate: 32000
};
// ** Info object:
const defaultInfo = {
name: '', // file name (auto-generated by default)
path: '', // path to the file (documents by default)
size: 0.0, // resulting file size in bytes
duration: 0.0, // sound duration in secs
userData: {} // data provided by the User as payload for saveAsFileRequest action
};
// *** Error object:
const error = {
errCode: 0, // error code, one of soundRecorderConstants.ERROR_(...) constants
details: { // arbitrary additional information
error: new Error(message) // Error object with optional message
... // additional optional data
}
};
Getting started
Step 1. Install mobile-rn-sound-recorder
$ npm install mobile-rn-sound-recorder --save
# or with yarn
$ yarn add mobile-rn-sound-recorder
Step 2. Install react-native-audio
If you have already installed react-native-audio as a dependency for your project you can skip this step. Otherwise please follow instructions provided by https://github.com/jsierles/react-native-audio.
Project Integration (2 steps)
Step 1. Add sound recorder's reducer to the root reducer
// rootReducer.js ;; const rootReducer = ; ;
or
// rootReducer.js ;; const rootReducer = ; ;
Step 2. Initialize & run sound recorder's saga
// rootSaga.js ;; { ;}
or
// rootSaga.js ;; { ;}
Usage in React Native components
Step 1. Screen
// components/VoiceRecorder/VoiceRecorderContainer.js ;;;; { return isRecording: soundRecorderSelectors isReadyToSave: soundRecorderSelectors currentTime: soundRecorderSelectors ... ;} { return ;} mapStateToProps mapDispatchToPropsVoiceRecorder;
// components/VoiceRecorder/VoiceRecorder.js static propTypes = ... cardId: recordId: PropTypesstring recordId: PropTypesstringisRequired isRecording: PropTypesbool isReadyToSave: PropTypesbool currentTime: PropTypesnumber mountRequest: PropTypesfuncisRequired unmountRequest: PropTypesfuncisRequired startRequest: PropTypesfuncisRequired stopRequest: PropTypesfuncisRequired saveAsFileRequest: PropTypesfuncisRequired ... ; static defaultProps = ... cardId: '' recordId: '' isRecording: false isReadyToSave: false currentTime: 00 ; { // Optional dictionary object that allows to re-write default messages // const dictionary = { // titleMicrophonePermission: 'Mikrofon freischalten', // msgMicrophonePermission: 'App benötigt Zugriff auf Ihr Mikrofon, sodass Sie etwas aufnehmen können', // msgNoPermission: 'Für die Aufnahme wurde der Zugriff nicht erteilt.' // }; // Note, that 1) the component will be drawn first time with the default props, // 2) It is more safe to initialize recorder somewhere earlier than here on componentDidMount() thisprops; } { thisprops; } ... thispropsisRecording ? thisprops : thisprops; { ... // fileInfo: passed 'name' as a file name w/o extension; default path (Documents) will be used // userData: passed 'title' and 'cardId' as a payload for ON_RECORDING_SAVED action (see Saga) thisprops; ... } ... { ... }... ;
Step 2. Saga
// saga/onSoundRecorder.js ;;; { try ... const name duration size userData = actioninfo; // Create DB record with information ; ... catch err console; } { try const errCode = actionerror; catch err } { ;} { ;} // Note: you could place soundRecorder.saga here in order to have all// sound recorder's sagas in one place { ;}