【问题标题】:want to show the number of the seconds of audio recorded by using react-native-audio-recorder-player想要显示使用 react-native-audio-recorder-player 录制的音频的秒数
【发布时间】: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


    【解决方案1】:

    请尝试在函数组件之外定义您的 audioRecorderPlayer 对象创建。喜欢

    const audioRecorderPlayer = new AudioRecorderPlayer();
    export default function ChatScreen({ navigation, user, route }) {
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-28
      • 2021-07-31
      • 1970-01-01
      • 2022-12-18
      • 2020-12-03
      • 2022-12-18
      • 2021-09-23
      • 1970-01-01
      • 2021-09-22
      相关资源
      最近更新 更多