React-native-sound: Get current time while playing

Created on 26 Jul 2017  路  2Comments  路  Source: zmxv/react-native-sound

How can I get the current time while playing

Most helpful comment

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

All 2 comments

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,
      });
    }
  });
}
Was this page helpful?
0 / 5 - 0 ratings