Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Running Audio in Background

Discussion in 'Android' started by Chromega1, May 7, 2014.

  1. Chromega1

    Chromega1

    Joined:
    Jan 8, 2014
    Posts:
    12
    Hey folks,

    I'm developing an app, and one of its features is the ability to play music. I would like my app to continue to play music when, for instance, the user goes to a lock screen. Is there any way to do this in Unity?

    I'm not sure if I'll have to step out of Unity and learn how to write Android plugins. If that is the path I need to take, any pointers for how I might get started?

    Thanks,
    -Mark
     
  2. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    I'm honestly not sure if you can do this from within Unity at all, but you can at least try AudioSource's ignoreListenerPause. On PC, that will continue to play audio even when the app is not in focus.
     
  3. Chromega1

    Chromega1

    Joined:
    Jan 8, 2014
    Posts:
    12
    Thanks for the suggestion but nope, no dice with AudioSource.ignoreListenerPause.
     
  4. Chromega1

    Chromega1

    Joined:
    Jan 8, 2014
    Posts:
    12
    I think I've got this resolved!

    I still need to poke around to see if I have all of the features I need, but I do have my Unity app playing music that continues on the lock screen. Some of this may be obvious to folks with more Android experience, but I'll explain it for other noobs like me, at least so people will know what to google for.

    Android has a notion of Activities and Services. Services can continue to run when you go to the lock screen, or minimize your application, but activities will run when the application has focus. Unity runs as an Activity.

    I created a new Java project in Eclipse, and created a new Activity that extends UnityPlayerActivity (I had to copy Unity's classes.jar into my project). I also created a Service class that plays music using Android's MediaPlayer object. My Activity mostly defers to the behavior of its base class, but it kicks off my Service, and provides hooks into it's PlaySong methods.

    I created a class in my Unity project that talks to my Activity. My Java Activity maintains a static reference to the Activity that just kicked off, and you can grab that static reference and call instance methods on it, letting me talk to my service.

    I had to export my Java project as a jar file and put it in Assets/Plugins/Android, and I had to update my Android manifest file to run my activity instead of Unity's and to kick off my service. I also had to add several permissions to the manifest, in my case, READ_EXTERNAL_STORAGE and WAKE_LOCK.

    It was actually pretty easy once I learned the vocabulary of things.

    -Mark
     
  5. twotheleone

    twotheleone

    Joined:
    Nov 18, 2014
    Posts:
    1
    Mark,

    Is there anyway you could post you Service and Activity Classes? It would be extremely helpful.

    Thank You
     
  6. Tekksin

    Tekksin

    Joined:
    Oct 20, 2013
    Posts:
    32
    Mark can you expound on any of that? Some of us aren't that advanced simply because we've never had to deal with jars and very little xml. What you said makes sense, but it's begging for more info
     
  7. latas

    latas

    Joined:
    Oct 9, 2013
    Posts:
    149
    It is true when you're working on Unity, you develop at a level where many low level things not need to be known, but if you need to create a new activity, be included in your AndroidManifest thru Unity, compiled, linked it, and invoked it from the code, that's not going to be as easy as see here some piece of code. Unless you want something to do copy & paste. My suggestion is to take a look to the Unity documentation about how to develop Plugins for Unity. That step will forced to you to understand the link between the Xcode (iOS), and Eclipse (Java-Android) tools, which you will need to develop plugins. For people, as me, who has developed games or apps, in Xcode or Eclipse for Android, before jump to Unity this level of understanding is easier, but I understand the limitations for others.
    That would be a good start point.
     
  8. JekaCorvus

    JekaCorvus

    Joined:
    Aug 20, 2014
    Posts:
    12
    -Mark

    Can you help me and put it somewhere or send me your plugin?
    Thank you!!!
     
  9. sub_

    sub_

    Joined:
    Mar 23, 2014
    Posts:
    3
    well, the years go by, but the problem stays.

    i need exactly what chromega1/ mark needed 2 years ago.
    today you cant write him private messages, thats why i bump this old thread.

    can someone share an plugin or another solution for that?
     
  10. PseudoGamer01

    PseudoGamer01

    Joined:
    Mar 9, 2017
    Posts:
    7
    Still nothing?
     
  11. diogo_gc

    diogo_gc

    Joined:
    Jul 13, 2015
    Posts:
    1
    That's what i did:
    I've created a BroadcastReceiver to set a bool with screen state. Then, on UnityPlayerActivity, at onCreate Method, instantiated a IntentFilter to get the Screen On / Off action and BroadcastReceiver as well.

    At onPause method, i check if it was called by a screen On / Off action, if so, only super on Pause is executed, if not,
    mUnityPlayer.pause() is executed as well.

    Maybe not the best practice, but works!

    BroadcastReceiver class:

    public class ScreenStateReceiver extends BroadcastReceiver {

    public static boolean setUnityPlayerOnPause = false;

    @override
    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

    setUnityPlayerOnPause = true;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
    setUnityPlayerOnPause = false;
    }
    }
    }

    UnityPlayerActivity:

    // Setup activity layout
    @override protected void onCreate(Bundle savedInstanceState)
    {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenStateReceiver();
    registerReceiver(mReceiver, filter);

    moveTaskToBack(true);
    mUnityPlayer = new UnityPlayer(this);
    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
    }

    @override protected void onPause()
    {
    super.onPause();
    if (!ScreenStateReceiver.setUnityPlayerOnPause) {
    System.out.println("SCREEN OFF OR LOCKED");
    } else {
    // THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
    mUnityPlayer.pause();
    }
    }
     
    camdarragh, BunnyChibi and spleendays like this.
  12. nareshbishtasus

    nareshbishtasus

    Joined:
    Jun 11, 2018
    Posts:
    36
    Hi, Do i have to use android studio for this solution or it can be done in unity itself?
     
    FrozenMangoRiaan likes this.
  13. FrozenMangoRiaan

    FrozenMangoRiaan

    Joined:
    Dec 19, 2021
    Posts:
    2
    I know diego gave an explanation, but it was very confusing. Has anyone figured out a solution to this to share please?