react-native-sound icon indicating copy to clipboard operation
react-native-sound copied to clipboard

Get current time while playing

Open cheesest3ak opened this issue 9 years ago • 4 comments

How can I get the current time while playing

cheesest3ak avatar Jul 26 '17 04:07 cheesest3ak

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>

riosimatupang avatar Jul 27 '17 09:07 riosimatupang

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

gvenk avatar Aug 25 '17 20:08 gvenk

eu consegui resolver assim sample

gustavoInventdigital avatar Sep 16 '21 17:09 gustavoInventdigital

@gustavoInventdigital Argument 1 (RCTResponseSenderBlock) of RNSound.play must not be null

RCTLogArgumentError(RCTModuleMethod*, unsigned long, objc_object*, char const*) RCTModuleMethod.mm:67 __41-[RCTModuleMethod processMethodSignature]_block_invoke.164 -[RCTModuleMethod invokeWithBridge:module:arguments:] facebook::react::invokeInner(RCTBridge*, RCTModuleData*, unsigned int, folly::dynamic const&, int, (anonymous namespace)::SchedulingContext) facebook::react::RCTNativeModule::invoke(unsigned int, folly::dynamic&&, int)::$_0::operator()() const invocation function for block in facebook::react::RCTNativeModule::invoke(unsigned int, folly::dynamic&&, int) _dispatch_call_block_and_release _dispatch_client_callout _dispatch_lane_serial_drain _dispatch_lane_invoke _dispatch_workloop_worker_thread _pthread_wqthread start_wqthread

getting this error since i'm new in react so i'm unable to figure out why the sound is null?

muhammaddawood129 avatar May 12 '22 06:05 muhammaddawood129