Unity audio roadblock

I am new to audio in Unity and have a basic goal that I cannot figure out how to achieve: I would like to play random music clips sequentially but pause/unpause them when the UI is active.

I have tried a coroutine, which is relatively new to me and it plays them sequentially but I can’t get it to pause/unpause correctly. I’ve tried async, which is totally new to me, and I can’t get that to work correctly.

I’ve read everything I can find and it seems that a major issue is that there is no way to check if an audio source is paused and, when it’s paused, isPlaying returns false. So, no matter how I approach it, I end up at a point where I simply cannot check for what I need to check for. I’ve implemented my own bool system (seems pointless) but still cannot figure out how to make this work.

I stripped away all of the logic I wanted to implement, like pre-loading a playlist based on BPM, loopability, and music key and now I’m just trying to get the basics to work. I’m using an Action to trigger the pause state. Here is my code, which is a mess and has obvious issues but I would appreciate any guidance or reference to a tutorial that addresses what I am trying to accomplish. Is the only solution to mute/unmute or reduce the volume, which will interrupt the flow of the music when returning to the game?

I should note that my audio is in scriptable objects that also contains the bpm, key, loopable setting, etc. So, that might help explain the code.

public AudioSource musicSource;
public AudioClip musicClip;
public int randomMusicInt = 0;
public bool isPaused;

private void OnEnable()
    TreatmentActions.a_PauseGame += PauseGame;
private void OnDisable()
    TreatmentActions.a_PauseGame -= PauseGame;

private void PauseGame(bool paused)
    else if (!paused)
        if(musicSource.clip!= null)
        else if (!musicSource.isPlaying || musicClip == null)
            randomMusicInt = Random.Range(0, music.Count);
            musicClip = music[randomMusicInt].soundAudio;
            musicSource.clip = musicClip;


Thank you! I’m sure this is just a basic misunderstanding on my part because it makes very little sense to me.

Try setting:

Time.timeScale = 0;
AudioListener.pause = true;

and when you resume game setting back:

Time.timeScale = 1;
AudioListener.pause = false;

Also use

  • Time.realtimeSinceStartup
  • Time.unscaledTime
  • WaitForSecondsRealtime

instead of

  • Time.time
  • Time.deltaTime
  • WaitForSeconds

while paused.