Loading Audio Files at Runtime for Playback and Processing


I am currently trying to implement something where (on Android, iOS & PC) mp3s are loaded at runtime and played back to the user. I also need to be able to access the raw PCM data to do some processing on it.

Using the WWW class is no option, because that eats memory like no other. (Even if that weren’t a problem, due to a bug in the getData method of AudioClip I can not access the raw data, once I’ve created an AudioClip from a file loaded through the WWW class. And then it also won’t read mp3s on some platforms)

I have tried various libraries and so far only paid ones worked (fmod, etc), but these are not an option for me. (I need an open source solution)

Currently trying to get ffmpeg to work, but none of the C# wrappers I have found seem to be any good. The neat thing about it is that it uses streams though, which greatly reduces memory usage especially when handling PCM data. …If I could just get it to work.

Does anyone have any experience in that regard? Help with either ffmpeg or another approach would be greatly appreciated! :slight_smile:


Update: Getting to the end of the project and thought I’d let everyone know how we worked around it.

As stated before, loading the entire song into unity through WWW eats memory. It allocates roughly 2x-3x the songs full PCM size (which for a 3 minute mp3 means about 2x-3x 60MB!).

So what we did was to compile mpg123 for ARMv7, find a C# wrapper for it and decode the mp3’s “by hand”. This was a bit of a hassle to get working, but it works and does so quick(ish). Because we use plugins this means that only pro/mobile users can do this though.

This approach limits us to solely mp3s (and WAVs), but due to licensing we wouldn’t be able to afford to decode m4a/aac anyway.