Search Unity

PlayerLoop while in background

Discussion in 'Android' started by r618, Apr 17, 2013.

  1. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Hello,

    I need to update the player while the app is in the background - this is possible on iOS via PlayerLoop call/s and works as expected.
    Is PlayerLoop reachable from an Activity on Android ?
    I've been trying (Unity 4.1.2) something like
    Code (csharp):
    1. static native boolean PlayerLoop (boolean a, boolean b, int p);
    which, surprisingly, gives
    java.lang.UnsatisfiedLinkError: Native method not found: com.company.product.MainActivity.PlayerLoop:(ZZI)Z

    Am I overlooking something || do I have to build native/ndk plugin and try to reach PlayerLoop this way ?

    Thanks!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    What you use there on iOS is pretty dangerous. Applications that go into background are meant to sleep. iOS does not guarantee their existance and runtime correctness when going there beyond what the 'background task' functionality offers on iOS and Apple actually has the freedom to reject your application or pull it after being approved for violating the ToS as your application qualifies as none of the allowed background application types.

    On Android you don't need this stuff.
    Simple activate 'run in background' in the playersettings and it will keep running.
    Though as on iOS, there is no guarantee that the application remains in existance when going into background as the OS might want to reclaim the RAM. Android uses services for background processing and similar things.
     
  3. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    If it has no background mode enabled then sure, it would be at least slightly risky; in my case I use background audio mode and since the logic of advancing to the next track ( it's an audio player ) is in Unity, I need to update it once in a while. I could probably rewrite the plugin to advance by itself, but not willing to right now I guess; so IMHO the application qualifies and hopefully they will not pull it from the store any time soon :) ( Before we happen to go into this further - no, I don't use Unity audio for this )
    You can have http://itunes.apple.com/us/app/trplr/id627600694&mt=8 a look after all, is should be pretty non resources hungry

    Where is this setting ? There is Run In Background for web and standalone -- only --.
    Once on the device e.g.

    Code (csharp):
    1. OnApplicationPause
    *gets* called so it is definitely not running in the background
    ( and yes, I googled my part about services )
     
    Last edited: Apr 17, 2013
  4. bitter

    bitter

    Unity Technologies

    Joined:
    Jan 11, 2012
    Posts:
    530
    r618, I'm not sure if you are being ironic, but just in case you're not; you can't add a native function to a random java class and expect it to work. There are a bunch of other things that are required to bind a managed code to native code.

    As you may have noticed "Run In Background" is not available for android. Also I don't think there is anyway right now to have the unity player run in the background. The main problem is that unity heavily relies on the gl thread. Basically the android gl thread is the thread that calls into the unity android version of PlayerLoop(), and android is setup in such a way that it is impossible to have the gl thread running while the application is paused. Even if you remove the actual pause() notification the thread will go into a stall when the gl surface is destroyed. Also, having a paused activity using a lot of memory will most likely mean the activity gets killed once the user starts flipping through other apps.

    The only solution that I see here is that you create a service that is in charge of playing and advancing through the tracks. Another reason for using the regular android api instead of relying on unity for playing music is that the android api will be require way less cpu to do so - no need for rendering or mixing audio.
     
  5. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    Hey bitter, thanks for reply!

    I assure you I'm not - I tried to copycat the mechanism from e.g. basslib and others where the native lib gets loaded by System.loadLibrary("sharedlib"); and class which loaded it <-- that's probably the important part -- has native calls available. Maybe there are other requirements for loading class and jni lib itself, but knowing pretty much nothing about Android stuff it was the last attempt from 'least hassle' solutions category

    That's what I was afraid of and tried to avoid in order to keep as much code in mono as possible, seems it's the only way for now;/

    Yeah, I already use native bits for playing modules as I mentioned earlier; this part was unfortunately clear quite soon since Unity refused streaming most of the tracks from disc ( but not all of them; but that's probably for another story )

    Thanks for the explanation !
    I really appreciate it since it's sometimes difficult to obtain any definitive info on 'corner cases' like this
     
    Last edited: Apr 17, 2013