0

我正在使用 react native 版本0.61.5和 react 版本16.8.3以及 react native 有天赋的聊天版本0.9.11来构建一个小型聊天应用程序。我想发送音频消息。所以我使用react-native-audio-recorder-player库来录制音频并播放。到目前为止,录音和播放是成功的。现在我想在用户播放音频时播放音轨进度。我使用react-native-community/react-native-slider来显示搜索栏。这就是音频消息的样子

在此处输入图像描述

当用户按下播放按钮时,我想在此滑块上显示音轨进度。到目前为止,我就是这样做的。

天才聊天组件

render() {
  return (
    <GiftedChat
      messages={this.state.messages}
      onSend={messages => this.onSend(messages)}
      scrollToBottom={true}
      renderUsernameOnMessage={true}
      renderComposer={this.renderComposer}
      renderBubble={this.renderBubble}
      user={{
        _id: 1,
        name:'You'
      }}
    />
  )
}

渲染气泡组件

renderBubble = (props) => {
  return(
    <View>
      <View style={{display:'flex',flexDirection:'row'}}>
        <Button iconLeft transparent onPress={this.onStartPlay}>
            <Icon name='ios-play' />
        </Button>
        <Slider
          style={{width: 150, height: 40}}
          minimumValue={0.0000}
          maximumValue={1.0000}
          value={this.state.audioProgress}
          minimumTrackTintColor="#FFFFFF"
          maximumTrackTintColor="#000000"
        />
      </View>
      <Bubble {...props}/>
    </View>
  )
}

onStartPlay 函数

onStartPlay = async () => {
  console.log('onStartPlay');
  const msg = await audioRecorderPlayer.startPlayer('sdcard/test_recording.aac');
  console.log(msg);
  audioRecorderPlayer.addPlayBackListener((e) => {
    if (e.current_position === e.duration) {
      console.log('finished');
      audioRecorderPlayer.stopPlayer();
    }
    this.setState({
      currentPositionSec: e.current_position,
      currentDurationSec: e.duration,
      playTime: audioRecorderPlayer.mmssss(Math.floor(e.current_position)),
      duration: audioRecorderPlayer.mmssss(Math.floor(e.duration)),
    });

    let currentProgress = Math.max(0,  e.current_position) /  e.duration;
    console.log('currentProgress',parseFloat(currentProgress).toFixed(4));
    this.setState({ audioProgress: parseFloat(currentProgress).toFixed(4) });
  });
};

我可以在控制台中看到当前进度正在更新audioProgress状态。我正在设置Slidervia 的值,audioProgress但搜索栏的位置没有得到更新。我正在 Nexus android 模拟器上测试这个。

4

1 回答 1

0

我相信您需要将 onValueChange 添加到 Slider。尝试添加:

<Slider
          style={{width: 150, height: 40}}
          minimumValue={0.0000}
          maximumValue={1.0000}
          value={this.state.audioProgress}
          onValueChange = {this.state.audioProgress.bind(this)} <===
          minimumTrackTintColor="#FFFFFF"
          maximumTrackTintColor="#000000"
        />

或者

<Slider
          style={{width: 150, height: 40}}
          minimumValue={0.0000}
          maximumValue={1.0000}
          value={this.state.audioProgress}
          onValueChange = {this.state.audioProgress} <===
          minimumTrackTintColor="#FFFFFF"
          maximumTrackTintColor="#000000"
        />

让我知道它是否有效!

于 2020-07-10T12:23:42.717 回答