'audio recording is not stopping after implementing "seconds counter state" in react native
In my react native chat application i add recording audio functionality, when start recording function in called i put the number of recorded seconds in state and showing them to user. but after doing this stop function in calling but the recording is not stopping. here is my code -
import React from 'react';
import { View, TouchableOpacity, Text} from 'react-native';
import AudioRecorderPlayer from 'react-native-audio-recorder-player';
const [rectime, setrectime] = useState(0)
export default function ChatScreen({ navigation, user, route }) {
const audioRecorderPlayer = new AudioRecorderPlayer();
const dirs = RNFetchBlob.fs.dirs;
const path = Platform.select({
ios: 'hello.m4a',
android: `${dirs.CacheDir}/hello.mp3`,
});
const onStartRecord = async () => {
await audioRecorderPlayer.startRecorder(path);
audioRecorderPlayer.addRecordBackListener(e => {
console.log('Recording . . . ', e);
setrectime(e.currentPosition / 1000)
});
};
const onStopRecord = async () => {
const audio = await audioRecorderPlayer.stopRecorder().then(() => {
})
return (() => {
alert('stop')
audioRecorderPlayer.removeRecordBackListener()
setrectime(0)
})
sendmp3(path)
};
return(
<View style={[styles.footer]}>
<TouchableOpacity activeOpacity={0.5} onPress={() => onStartRecord()} style=
{styles.btnSendd}>
<Ionicons name={'mic-circle-outline'} size={24} color={'#000'} />
</TouchableOpacity>
<TouchableOpacity activeOpacity={0.5} onPress={() => onStopRecord()} style=
{styles.btnSendd}>
<Ionicons name={'mic-circle-outline'} size={24} color={'#efc100'} />
</TouchableOpacity>
{
rectime > 0
?
<View style={styles.rectime}>
<Text style={{ fontSize: 15 }}>{rectime.toFixed(0)}</Text>
</View>
:
<>
<TouchableOpacity activeOpacity={0.5} onPress={handleVideoPicker} style=
{styles.btnSendd}>
<Ionicons name={'videocam-outline'} size={24} color={'black'} />
</TouchableOpacity>
<TouchableOpacity style={styles.btnSendd} activeOpacity={0.5} onPress=
{handlePhotoPicker}>
<Ionicons name={'camera-outline'} size={24} color={'black'} />
</TouchableOpacity>
</>
}
)
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
