Audio plays but limited count

#1

Hello,

I’m newbie to react-native and Expo and first off please guys keep on. This is great product.

Now, for app I’m trying to build there should be number of different sounds to play. Playing is bound to click
event on TouchableHighlight.

When user make couple of clicks it works fine for a while. However, exactly after 14 attempts, each
15th click simply won’t play any sound. It is tested both on Genymotion device running Android 7 or on
cellphone running Android 6.

I’m confused as there are no errors or logs shown but there is no sound anymore. Only after Expo client
is restarted app plays sound again.

My code for playing a sound is based on minimal sound example found on https://repl.it/J7i6/48.

Actual part that matters is very simple:

onPress={async () => {
const source = {
uri: “http://www.slspencer.com/Sounds/Chewbacca/Chewie3.mp3
};

        try {
          await Audio.setIsEnabledAsync(true);
          const sound = new Audio.Sound();
          await sound.loadAsync(source);
          await sound.playAsync(); 
        } catch(error) {
          console.error(error);
        }
      }}

My very remote guess is that Expo client is limited or that some resource cleanup is missing. Finally, provided minimalist example reproduce the same behavior.

Thanks.

#2

Just solved it but leaving it here in case someone else is reckless as I was.
It was about resource management, and somehow I managed to miss unloadAsync function whatsoever.

So solution was to keep track of actual Sound object instance, and bind to its onPlaybackStatusUpdate event.
From here it was easy as Status object being passed to event handler actually contains a lot of info inluding
didJustFinish flag. So in handler it is something like:

if (status.didJustFinish && this.playbackInstance !== null) {
this.playbackInstance.unloadAsync();
}

Additional help was reading from another post: https://forums.expo.io/t/audio-trying-to-update-unmounted-component-on-android/3850