I have a Voice URL I got it from Database and want to play it using react-native-sound,
So I make a reusable component that renders a play icon and duration "without animation right now"
so after the sound play, I change play icon first then run setInterval
to increase count every second +1
but it's too slow at first to change icon and the real duration in seconds: 11 but the timer stops in 9
So how can I make it faster...
Code
this.state = {
playingNow: false,
spinValue: new Animated.Value(0),
timer: 0,
};
// get URL sound from DB then play it :)
play = async () => {
const {VoiceURL} = this.props;
console.log('Play Func Object:', VoiceURL);
const sound = new Sound(VoiceURL, '', error => {
if (error) {
console.log('failed to load the sound', error);
return;
} else {
// Animated.timing(this.state.spinValue, {
// toValue: 1,
// duration: sound. _duration * 1100 || 1000,
// easing: Easing.linear,
// useNativeDriver: true,
// }).start(() => {
// this.state.spinValue.setValue(0);
// });
this.interval = setInterval(
() =>
this.setState({
playingNow: true,
timer: this.state.timer + 1,
}),
1000,
);
sound.play(success => {
if (success) {
// That's mean sound is finish
console.log('success?', success);
this.setState(
{playingNow: false, timer: 0},
() => sound.stop(),
clearInterval(this.interval),
);
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
}
});
console.log(
'duration in seconds: ' +
sound.getDuration() +
'number of channels: ' +
sound.getNumberOfChannels(),
);
sound.getCurrentTime(seconds => console.log('at: ' + seconds));
}
sound.setNumberOfLoops(0);
});
console.log('in_play??', sound);
return sound;
};
That's render a UI
_renderPlayButton = onPress => {
const {timer} = this.state;
let icon = this.state.playingNow ? 'pause' : 'play-arrow';
// const spin = this.state.spinValue.interpolate({
// inputRange: [0, 1],
// outputRange: [0, -120],
// });
return (
<View
style={{
opacity: this.props.opacity,
alignItems: 'center',
flexDirection: 'row',
}}>
<TouchableOpacity
style={{backgroundColor: '#1E558E', borderRadius: 100}}
onPress={onPress}>
<Icon name={icon} color="#fff" size={25} style={{padding: 2}} />
</TouchableOpacity>
<View
style={{flexDirection: 'row', marginLeft: 5, alignItems: 'center'}}>
{/* <Animated.View
style={{
backgroundColor: '#f00',
borderRadius: 10,
width: 15,
height: 15,
zIndex: 10,
transform: [{translateX: spin}],
}}
/> */}
<View
style={{
width: 120,
backgroundColor: '#a7a7a7',
height: 2,
}}
/>
<Text style={{color: '#777', marginBottom: 5, marginLeft: 10}}>
00:
{('0' + (timer || 0)).slice(-2)}
{console.log('timer-state:', timer)}
</Text>
</View>
</View>
);
};
JSX
<View>
{this._renderPlayButton(() => {
this.play();
})}
</View>