unity-background-service
unity-background-service copied to clipboard
Run music app in the background
Hi, I've built a unity app for android that plays music for table top rpgs. It's pretty simple....click an image and it plays a series of random tracks and songs to suit the mood.
If the app loses focus (user changes app or the screen goes to sleep) the phone stops playing music.
Do you know how I can make a service to keep playing the music ?
I'm relatively new to Unity and Android.
Thanks in advance for any ideas. Anthony
Dude laid it out pretty clear in the description of this project...
The only solution is to let the service work on foreground as a notification and then the users are able to hide that notification if they wants to.
Running in the foreground as a notification sounds great....if I understood how. I have several noob questions.
- On Awake the SendActivityReference method is called. Understood.
- It creates the Unity Android class and Unity activity.
What are these? - Then it sends the current activity to the plugin. What is the plugin? Do I need to use this plugin in my app ? Or do I need to make a new one ?
- StartService and StopService methods respectively starts and stops the background service as well as GetCurrentSteps method. I think I want my background service to start when the Unity loses focus (when the user changes app or screen goes to sleep). How do I make my service start when focus is lost.
- I think I can ignore stuff about step counting.
- The entry point of the Android application is the Bridge class. What do I do with the Android application ? Do I edit it in Android Studio to suit my needs ? Do I need to upload the Android application to the google play console ? I've only used .aab from Unity to upload to the play console.
- How do I make my app only play music in the background...and not the image effects that I have implemented ?
- I want the background service to stop if the user closes my app. How do I do that ?
Were you able to do what you set out to do?
No. All questions remain.
I can provide all answers
And....what are those answers.
Unity Action is basically a way to trigger events https://docs.unity3d.com/ScriptReference/Events.UnityAction.html
An Android Class: Instance of the class Class represent classes and interfaces in a running Java application. https://developer.android.com/reference/java/lang/Class
The action is used to trigger the Android Class as I understand it.
The plugin is for Unity to use native Android Libraries to add native function. https://docs.unity3d.com/550/Documentation/Manual/PluginsForAndroid.html
I have not loaded this project yet but it looks like it is all setup... have you tried to just run this project as it is to get a better understanding?
The android code base looks like you need to load it in Android studio and properly sign it
This source is not for audio (though it can be used that way...) This is meant to create an Android Service and run it in a Unity based app Like this: https://developer.android.com/guide/components/services
It looks like the service is setup to manually be started and stopped...
You sound like you want it manually started with option of manual stop but you will also it sound like you want want to do something like https://github.com/nintendaii/unity-background-service/blob/c1969090ab8375cdca0d371b26f3d644eab7cae0/Unity3DProject/Assets/Scripts/BackgroundService.cs#L46
do that in your OnDestory or something
you want to use something like this for your foreground service... or at least to get an idea of how you will need to change the android code base to suit your needs...
hope that helps... have a good day
this stackoverflow seems to be more to the point:
https://stackoverflow.com/a/58899640/1815624
Update https://github.com/ffrxsln/unity-background-service-updated/releases/tag/0.1