Unity3D and background audio

I recently developed an app for a friend that plays a lot of background audio for both iOS and Android.

For iOS, it was pretty easy to just use the normal Unity3D audio methods, and even when the app was minimized, or the device was sleeping, the audio would continue in the background and even follow looping and the SetScheduledEndTime settings.

However, for Android the situation was very different. Audio would stop playing when the App was minimized, or the device was put in sleep mode. This was true even when I set the background flag.

I ended up getting around this by creating a NativeAudio class that hooked into a plugin for the Android version. This plugin used the native Android methods associated with the MediaPlayer class. I also moved all the audio files to the Streaming Assets folder so the native player could access them.

For iOS I created a version of the NativeAudio class that just hooked back into theĀ  standard AudioSource class, but i modified the loading of the AudioClips to ‘download’ them from the Streaming Assets folder.

Maybe one day I’ll put together a video tutorial on this.

Leave a Reply