react-native - stopRecorder() 不起作用 - react-native-audio-recorder-player

标签 react-native audio hybrid-mobile-app mobile-application

我正在尝试使用包 react-native-audio-recorder-player 在 React Native 应用程序中录制音频。录音开始成功,但在调用 stopRecorder() 后仍继续录音。

尝试了来自 gitHub 的多种解决方案,但没有任何帮助。这是我的代码

import React from 'react';
import { View, TouchableOpacity, Text} from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';

export const Recorder = () => {
 const audioRecorderPlayer = new AudioRecorderPlayer();

 const onStartRecord = async () => {
  await audioRecorderPlayer.startRecorder();
  audioRecorderPlayer.addRecordBackListener(e => {
    console.log('Recording . . . ', e.current_position);
     return;
    });
  };

 const onStopRecord = async () => {
  const audio = await audioRecorderPlayer.stopRecorder();
  audioRecorderPlayer.removeRecordBackListener();
  };

 return (
  <View style={{flex: 1, justifyContent: 'center', alignItems: 'space-between'}}>
   <TouchableOpacity onPress={onStartRecord}>
     <Text>Start</Text>
   </TouchableOpacity>

   <TouchableOpacity onPress={onStopRecord}>
     <Text>Stop</Text>
   </TouchableOpacity>
  </View>
 );
};

即使按下停止后,控制台仍然在进行 Recording 。 . .,下面是我的控制台。

enter image description here

最佳答案

为了这个工作,需要在组件外添加const audioRecorderPlayer = new AudioRecorderPlayer();。因此,组件将如下所示。

import React from 'react';
import { View, TouchableOpacity, Text} from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';

const audioRecorderPlayer = new AudioRecorderPlayer();

export const Recorder = () => {
 const onStartRecord = async () => {
  await audioRecorderPlayer.startRecorder();
  audioRecorderPlayer.addRecordBackListener(e => {
    console.log('Recording . . . ', e.current_position);
     return;
    });
  };

 const onStopRecord = async () => {
  const audio = await audioRecorderPlayer.stopRecorder();
  audioRecorderPlayer.removeRecordBackListener();
  };

 return (
  <View style={{flex: 1, justifyContent: 'center', alignItems: 'space-between'}}>
   <TouchableOpacity onPress={onStartRecord}>
     <Text>Start</Text>
   </TouchableOpacity>

   <TouchableOpacity onPress={onStopRecord}>
     <Text>Stop</Text>
   </TouchableOpacity>
  </View>
 );
};

进行此更改后,我的 stopRecorder 工作正常。

关于react-native - stopRecorder() 不起作用 - react-native-audio-recorder-player,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64353238/

相关文章:

android - 在android的videoview中同时播放单独的视频和音频

c++ - 实时音频(发送和接收)的好库?

python - 我可以在 Ionic 中使用 Python 进行后端工作吗

javascript - React Native 中使用 AsyncStorage 的异步辅助函数

javascript - 如何在 React Native 的 <Text> JSX 标签中渲染字符串中的变量

react-native - React Native svg 未正确渲染

matlab - 停止使用dsp.AudioRecorder在用户提示下进行音频录制?

android - Ionic 移动应用程序是否可以免费构建生产级别?

javascript - React Native 中的 redux-form 有问题

react-native - 文件下载不适用于 IOS 的 native react