Ravikiran kallepally
Ravikiran kallepally

Reputation: 31

Is there a way to record audio with React native and store it as a wave file?

when the user clicks the button, The app should start listening the audio and stop the recording when the user is silent(say for 20 seconds after silence) , now store that audio file in a wave format(eg: test.wav) in react native ?

Upvotes: 3

Views: 11848

Answers (2)

user8376713
user8376713

Reputation: 445

If you are also interested in creating file from this recording in the user device, i recommend using react-native-audio i personally tried in android and it worked pretty smoothly, example of code:

/* on top of component:*/
import {AudioRecorder, AudioUtils} from 'react-native-audio';
/*all your other imports*/
class Example extends Component {
     constructor(props) {
         super(props);
         this.AudioRecorder = AudioRecorder
       }
     startRecord(){
         let folder =  AudioUtils.DocumentDirectoryPath;
         let audioPath = folder +'/myFile.wav';
         let options=   { SampleRate: 22050,
            Channels: 1,
            AudioQuality: "Low",
            AudioEncoding: "wav",
            MeteringEnabled: true,};
        this.AudioRecorder.prepareRecordingAtPath(audioPath,options).then(
              ((success)=>{ this.AudioRecorder.startRecording((success)=>{
                                 }).catch((err)=>{})
             }).catch(err=>{})


       }

      stopRecord(){
          this.audioRecorder.stop()
       }

 }

you will probably also need user permission to use the device microphone, for this i recommend react-native-permissions

Upvotes: 1

Hadi Mir
Hadi Mir

Reputation: 5123

As you already know that there is no inbuilt feature to record audio in React Native, However you can always use third part Library/package. There are number of them available on NPM. Here is one of them react native audio record

import AudioRecord from 'react-native-audio-record';
 
const options = {
  sampleRate: 16000,  // default 44100
  channels: 1,        // 1 or 2, default 1
  bitsPerSample: 16,  // 8 or 16, default 16
  audioSource: 6,     // android only 
  wavFile: 'test.wav' // default 'audio.wav'
};
 
AudioRecord.init(options);

//Start Recording
AudioRecord.start();
 
 
//Stop Recording
AudioRecord.stop(); 
 

Hope this help

Upvotes: 1

Related Questions