我正在使用 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
状态。我正在设置Slider
via 的值,audioProgress
但搜索栏的位置没有得到更新。我正在 Nexus android 模拟器上测试这个。