Ionic-native-audio unloading file after it finishes playing?

So I’m running into an issue on Android where I preload complex 15+ audio files on a page and have tap to play events tied to different files. The first 14 play fine, regardless of which files are played, and each can be played multiple times with no issues.

When the 15th is tapped and any after that it doesn’t play any audio until you go back and then reload the page.

I did some research and I believe that this is related to limits on how many audio files that Android devices can have loaded simultaneously (my files are ~1 second and only a few KB, so it’s not a space issue but limitation on number of files).

Is there an easy way that I can have a tap() even linked to a play command for native-audio (i.e.:(tap)=“playt20a1()”) and have the playt20a1 typescript load, play, and then unload these small audio files?

I tried

playt20a1() {
			this.nativeAudio.preloadComplex('t20a1', 'assets/aud/t20a1.mp3', 1, 1, 0);'t20a1');

This causes issues because the unload command occurs right after the play, I would love it if I could unload it after the audio file finishes or after a 3-5 second delay to ensure it plays completely. The documentation doesn’t really detail timeout functions so I was wondering if there was another way around this issue.


OK I’ve been working on this and have a working, but maybe not super clean, solution. I load, set 0.1 second timeout and then play, and then wait 3 seconds to unload. This gets around the android opencore issue and allows me to have as many small files on a single page as I want.

playt20a1() {
				this.nativeAudio.preloadComplex('t20a1', 'assets/aud/t20a1.mp3', 1, 1, 0);
				setTimeout(() => {'t20a1');},100);
				setTimeout(() => {this.nativeAudio.unload('t20a1');},3000);

the play method has a callback param which you can use to unload.
e.g:, () => { this.unload(audioFile); });

the this.unload(audioFile) above will only execute once the file has completed playing.

also for shorter files look into using preloadSimple

1 Like

Awesome this is perfect since my delay workaround was a pretty shoddy fix.

I started with preload simple but had to switch to complex because preload simple is broken on iOS and the plugin hasn’t been updated in quite a while even though community members have been making pull requests on FloatingHotPot’s repo.

Thanks so much for letting me know about the callback!

Awesome. Glad I could help.

1 Like