【问题标题】:stopRecorder() is not working - react-native-audio-recorder-playerstopRecorder() 不工作 - react-native-audio-recorder-player
【发布时间】:2021-01-28 20:32:02
【问题描述】:

我正在尝试使用包 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>
 );
};

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

【问题讨论】:

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


    【解决方案1】:

    为此,需要在组件外添加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 工作正常。

    【讨论】:

    • 我遇到了同样的问题。以上答案对我有帮助。谢谢兄弟
    猜你喜欢
    • 2022-08-20
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    • 2022-01-21
    • 2017-12-16
    • 2022-01-06
    相关资源
    最近更新 更多