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

Is there any way to loadUrl() async?

Open ty01314 opened this issue 5 years ago • 4 comments

thank you for your plugin.

I have a problem during call loadUrl() in componentDidMount(). React native start render() until loadUrl() finish.

Hence, May I ask is There any way to loadUrl() async?

ty01314 avatar Nov 28 '19 06:11 ty01314

All native call that goes through React Native bridge are async:

React Native bridge is asynchronous, so the only way to pass a result to JavaScript is by using callbacks or emitting events

as you can find in the official documentation here.

However the code could be using up some CPU power causing the render to delay. Does removing the loadUrl() completely removes the delay? Also which platform are you on?

johnsonsu avatar Nov 28 '19 11:11 johnsonsu

tried with async/await function but no different.

Does removing the loadUrl() completely removes the delay? yes.

I am on Android platform.

Thank you so much~~

ty01314 avatar Nov 28 '19 11:11 ty01314

This is very strange as componentDidMount() should run after the first render() as discussed here.

In that case, the only hack that I can think of is try using a setTimeout(), like:

...
componentDidMount() {
  setTimeout(() => {
    loadUrl()
  }, 500)
}
...

johnsonsu avatar Nov 28 '19 13:11 johnsonsu

I think the problem is is not with React, but this prepare call on the native side. Per the docs this "prepares the player for playback, synchronously". For a remote url the app basically becomes unresponsive during the time it takes to download the file.

This call to create would have the same problem because create automatically calls prepare.

There is a prepareAsync method that would probably be better to use here. I don't see any create overload that uses prepareAsync though. Might need to use the constructor instead and configure it manually, like in this example.

AdamStone avatar Sep 14 '22 05:09 AdamStone