Skip to content

Play audio on mobile even when the game is suspended in the background

License

Notifications You must be signed in to change notification settings

Wenrong274/UnityBackgroundAudio

 
 

Repository files navigation

UnityBackgroundAudio

Play audio on mobile even when the game is suspended in the background

Dependencies

The following plug-ins are needed for UnityBackgroundAudio to work.

  • Unity Version 2019.4 or later

  • Unity-Swift - Github

  • Unity-Jar-Resolver Github

Usage

Download and import the dependencies and the .unitypackage from Releases

// Get an instance of the BackgroundAudioImplementation class for the current build platform
var instance = BackgroundAudioManager.NewInstance();

...

// To play an mp3 file
// NOTE: Network playback currently not supported
instance.Play("/Path/To/File.mp3");

...

// Callbacks
// NOTE: Callbacks on Android are not invoked on the main thread (use a main thread dispatcher to update UI)
instance.OnAudioStarted += () => Debug.Log("Audio started playing");
instance.OnAudioStopped += () => Debug.Log("Audio stopped playing");
instance.OnAudioPaused += () => Debug.Log("Audio paused");
instance.OnAudioResumed += () => Debug.Log("Audio resumed");

Caveats (Android)

Notification

The plugin starts a ForegroundService whenever an audio is played. This is required so that even if Android decides to kill the game the audio continues to play. To change the icon, simply replace the ba_notification_icon.png inside Assets/Plugins/Android/res/drawable/ with your own icon.

Threading

AndroidJNI.AttachCurrentThread(); must be called before calling any methods from outside the main thread and AndroidJNI.DetachCurrentThread(); must be called before the thread closes.

Sample

Clone or download the repo as .zip and open the SampleScene scene under Assets/Scenes/

Notes

I am still learning Native Plugin development for Android and iOS so there might be inefficient/bad code in the .java or .swift files. Feel free to criticize or submit pull requests.

About

Play audio on mobile even when the game is suspended in the background

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 43.7%
  • Java 38.9%
  • Swift 13.3%
  • Objective-C++ 4.1%