简体   繁体   English

有没有办法用 React Native 录制音频并将其存储为波形文件?

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

当用户单击按钮时,应用程序应开始收听音频并在用户静音时停止录音(例如静音后 20 秒),现在以波形格式(例如:test.wav)存储该音频文件以进行反应本国的 ?

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.正如您已经知道的那样,在 React Native 中没有录制音频的内置功能,但是您始终可以使用第三方库/包。 There are number of them available on NPM. 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希望这有帮助

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:如果您也有兴趣在用户设备中从此录音创建文件,我建议使用我在 android 中亲自尝试过的react-native-audio并且它工作得非常顺利,代码示例:

/* 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您可能还需要用户许可才能使用设备麦克风,为此我建议使用react-native-permissions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM