【发布时间】:2022-08-20 21:45:11
【问题描述】:
我试图显示录制开始时的秒数,但在开始录制后我正在更新状态,但是在单击停止按钮状态更新后这不起作用,功能正在调用但录制没有停止。
我尝试了所有可能的事情,但没有任何帮助。这是我的代码
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>
</>
}
)
}
标签: reactjs react-native react-hooks