I am new to React and currently developing the music player using React Js. The idea is to change the seek-bar as the song plays and the user can change the track position using seek-bar(like any other media players). I have the JSX audio tag to play the song and input tag as a seek-bar. Please refer to the below code segments.
{/* audio tag */}
<audio ref={songRef} type="audio/mpeg" preload="true"
onTimeUpdate={ e => setCurrentTime(e.target.value) }
onCanPlay={ e => setDuration(e.target.duration) }
onEnded={ e => next_song(e, allSong.indexOf(currentSong) + 1, state) }
src={ currentSong && `${dev_url}${currentSong.id}` }
/>
{/* audio seek-bar */}
{ (currentSong || pSong) &&
<input type="range" style={styles.progress}
value={duration ? (currentTime * 100)/duration : 0}
onChange={onChangeProgress}/>}
Everything works well except the onChange event on seek-bar. Every time I triggered the onChange function (i.e; when I'm trying to change the track position using seek-bar), the song goes back to the start (00:00), instead of the desired position. Below is my onChangeHandler which is used to update seek-bar and track position.
function onChangeProgress(event) {
let time = (event.target.value * duration) / 100;
setCurrentTime(time);
songRef.current.currentTime = time;
}
I have been stuck here for few days already. Please help me?