Forum rules - please read before posting.

Wrong current track returned after game regains focus

I'm having problem determining what music and ambience is being played (using a Sound:Play Music action). The following returns the correct ID until the game looses focus and then regains it, after which it always returns -1:
KickStarter.stateHandler.GetMusicEngine().GetCurrentTrackID();

Latest version of AC, Unity 2020.1.3

Comments

  • As in, alt-tabbing to the desktop and back? It may be a Unity issue.

    On what platform, and is the music itself any different?

  • Yes, alt-tabbing to another app and then and back. The music playback is fine, same music is playing correctly, yet the method returns -1. I'm on Windows.
  • This is in a build, I take it.

    When are you calling this method? I can't recreate such an issue.

  • edited August 2020

    I'm calling it when the player have left the current scene and a new scene has loaded (in order to see if I should play a new song or keep the same one going). Try calling it after a new scene has loaded and it should be fine. Then try unfocusing the app before you leave the scene, the next time you leave the scene it should return -1.

  • edited September 2020

    Via an update method, or an event call?

  • edited September 2020
    Try it on your scene changed event
  • Nothing found, I'm afraid.

  • edited September 2020

    Ok, I researched this and found the reason:

    When alt tabbing from a playing game (you can do this when playing in the Unity editor), Sound.IsPlaying() returns false, causing line 105 in Soundtrack.cs to remove it from the queuedSoundtrack list (therefore returning -1 on GetCurrentTrackID()).

    The reason is that audioSource.isPlaying returns false when the game is paused (I see you left a comment about a similar issue). The way I found to fix it is to add the following check:

    if (isAppPaused && audioSource.time > 0f)
        return true;
    

    To know whether the app is paused (Kickstarter.stateHandler.IsPaused won't have kicked in yet), I also had to add this to Sound.cs:

    bool isAppPaused;
    void OnApplicationPause(bool pauseStatus)
    {
        isAppPaused = pauseStatus;
    }
    
  • Excellent work. Though I'm still unsure why I couldn't recreate.

    I'll look into the suggestions, thanks.

  • Here's some more info that might help you recreate:

  • Recreated, the crucial step appears to be the unchecking of "Run In Background" in the Player Settings.

  • edited September 2020

    That's great 👍

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.