Search Unity

Question Changing audio clips from separate thread/during sleep

Discussion in 'Audio & Video' started by push-pop, Jul 10, 2020.

  1. push-pop

    push-pop

    Joined:
    Jun 29, 2017
    Posts:
    28
    Hi all!

    Trying to nail down a workflow for a mobile (iOS and Android) audio app I'm currently developing. It requires changing audio clips while the app is paused based on environmental data from the phone (running as background services.)

    For iOS I've figured out how to get background audio playing, and can pause/play via lock screen. The audiosources will loop, but none of the logic runs to change the clips.

    For Android, background audio is a bit trickier and the only way I figured out how to do it is to not pause the Unity app, which I think is really bad practice.

    I was thinking that I can trigger everything from the device callbacks, but then we're on a different thread, which seems to be a dead end in terms of Unity audio (like most Unity APIs.)

    Is native audio my only path forward here? Basically I just need a simple looping audio player but I really need methods like PlayScheduled, and I'm not sure how difficult it would be to implement that myself in native code.

    I've taken a look at this solution, but it doesn't seem suited to scheduled play, more like super low latency (which might work for me as well.)

    Any other thoughts on how to approach this problem before I dive in and write my own native audio libs?

    Thanks!
    Nate