简体   繁体   English

反应原生 slider 不更新 state 更新的进度

[英]React native slider not updating progress on state update

I'm using react native version 0.61.5 and react version 16.8.3 with react native gifted chat version 0.9.11 to build a small chat app.我正在使用 react native 版本0.61.5和 react 版本16.8.3以及 react native 有天赋的聊天版本0.9.11来构建一个小型聊天应用程序。 I want to send audio messages.我想发送音频消息。 So I use react-native-audio-recorder-player library to record audio and play it.所以我使用react-native-audio-recorder-player库来录制音频并播放。 So far audio recording and playing is a success.到目前为止,录音和播放是成功的。 Now I want to play the audio track progress when user play the audio.现在我想在用户播放音频时播放音轨进度。 I used react-native-community/react-native-slider to display a seekbar.我使用react-native-community/react-native-slider来显示搜索栏。 This is how the audio message look like这就是音频消息的样子

在此处输入图像描述

When user press the play button I want to show the audio track progress on this slider.当用户按下播放按钮时,我想在此 slider 上显示音轨进度。 So far this is how I did it.到目前为止,我就是这样做的。

Gifted Chat component天才聊天组件

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 component渲染气泡组件

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 function onStartPlay function

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) });
  });
};

and I can see in the console current progress is getting updated in the audioProgress state.我可以在控制台中看到当前进度在audioProgress state 中得到更新。 And I'm setting the value of the Slider via the audioProgress but the position of the seek bar is not getting updated.我正在通过audioProgress设置Slider的值,但搜索栏的 position 没有得到更新。 I'm testing this on Nexus android emulator.我在 Nexus android 模拟器上对此进行测试。

I believe you need to add the onValueChange to the Slider.我相信您需要将 onValueChange 添加到 Slider。 Try adding either:尝试添加:

<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"
        />

or或者

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

Let me know if it works!让我知道它是否有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM