How can I get the current time while playing
Hi @cheesest3ak, same here, I am wondering how to get the current time while playing.
But it should be as simple as this:
whoosh.play(() => {
// Get the current time
whoosh.getCurrentTime((seconds) => {
this.setState({
currentTime: seconds
})
});
});
constructor(props) {
super(props);
this.state = {
currentTime: 0
};
}
But when every time I get the current time, it always returns -1
<Text>{this.state.currentTime}</Text>
The callback in the play-method, is an onEnd-callback, so this only gets called after the file has finished playing or an error occurred.
I'm using a timer to get the currenttime, something like this:
play() {
this.tickInterval = setInterval(() => { this.tick(); }, 250);
this.s.play((success) => {
if (success) {
if (this.tickInterval) {
clearInterval(this.tickInterval);
this.tickInterval = null;
}
} else {
if (this.tickInterval) {
clearInterval(this.tickInterval);
this.tickInterval = null;
}
console.log('error');
}
}
tick() {
this.s.getCurrentTime((seconds) => {
if (this.tickInterval) {
this.setState({
currentTime: seconds,
});
}
});
}
Most helpful comment
The callback in the
play-method, is anonEnd-callback, so this only gets called after the file has finished playing or an error occurred.I'm using a timer to get the currenttime, something like this: