react-native-audio-recorder-player icon indicating copy to clipboard operation
react-native-audio-recorder-player copied to clipboard

stopRecorder() function doesn't work on Android 7 when it uses react-native-voice

Open jalinegm opened this issue 3 years ago • 2 comments

Please fill the template to help you out. Also, please try the Example project compare before submiting the issue when you have certain issue with your project setup.

Version of react-native-audio-recorder-player 3.2.1

Version of React Native 0.63.4

Platforms you faced the error (IOS or Android or both?) Android 7

I'm using the recorder in parallel with react-native-voice. On Android 8 e 10 it's working well, start, stop. However, on Android 7 the function stopRecorder() doesn't work. The message on catch is "Error not specified." My const is outside and I tried to use callback too...

Steps to reproduce the behavior

import React, { useState, useEffect } from "react";
import { View, Button, PermissionsAndroid } from "react-native";
import Voice, {
  SpeechRecognizedEvent,
  SpeechResultsEvent,
  SpeechErrorEvent,
} from "@react-native-voice/voice";

import AudioRecorderPlayer from "react-native-audio-recorder-player";

const audioRecorderPlayer = new AudioRecorderPlayer();

const { forwardRef, useRef, useImperativeHandle } = React;

const Microphone = forwardRef((props, ref) => {
  const setState = (state) => {
    props.onSetState(state);
  };

  useImperativeHandle(ref, () => ({
    async startRecognizing() {
   
        try {
          await Voice.start("pt-BR");
        } catch (e) {
          console.error(e);
        }
    },

    async stopRecognizing() {
      setState({
        ...props.onState,
        recording: false,
        audio: true,
      });
      try {
        await Voice.stop();
      } catch (e) {
        console.error(e);
      }
    },
  }));

  useEffect(() => {
 
    const onSpeechStart = (e) => {
      try {
        recordStart();
        setState({
          ...props.onState,
          recording: true,
          audio: false,
          playing: false,
        });
      } catch (e) {
        console.error(e);
      }
    };

    const onSpeechEnd = (e) => {
      try {
        stopRecord();
        setState({
          ...props.onState,
          recording: false,
          audio: true,
        });
      } catch (e) {
        console.error(e);
      }
    };

    Voice.onSpeechStart = onSpeechStart;
    Voice.onSpeechEnd = onSpeechEnd;

    return () => {
      Voice.destroy().then(() => Voice.removeAllListeners());
    };
  }, []);

  const recordStart = React.useCallback(async () => {
    const result = await audioRecorderPlayer.startRecorder();
  }, []);

  const stopRecord = React.useCallback(async () => {
    try {
      const result = await audioRecorderPlayer.stopRecorder();
      audioRecorderPlayer.removeRecordBackListener();
    } catch (e) {
      console.error("erro ao parar gravacao " + e);
    }
  }, []);

  const destroy = async () => {
    try {
      await Voice.destroy();
      await audioRecorderPlayer.removePlayBackListener();
      await audioRecorderPlayer.removeRecordBackListener();
    } catch (e) {
      console.error(e);
    }
  };

  return (
    <View>
      {/* <Button onPress={start} title="Start" color="#841584" />
      <Button onPress={stop} title="Stop" color="#822222" /> */}
    </View>
  );
}); 

export default Microphone;

jalinegm avatar Nov 20 '21 21:11 jalinegm

Same issue on IOS, getting the error: 'Already stopped'

pnadj avatar Feb 03 '22 09:02 pnadj

since android does not allow you to use mic with 2 different components, react-native-voice and audio-recorder could not be used at the same time. this is not only for audio-recorder but it is also affecting camera recorders too.

on ios, you can use those two at the same time.

AlkanV avatar Aug 30 '22 10:08 AlkanV