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
It looks like you're new here. If you want to get involved, click one of these buttons!
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?
This is in a build, I take it.
When are you calling this method? I can't recreate such an issue.
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.
Via an update method, or an event call?
Nothing found, I'm afraid.
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:
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:
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:
https://1drv.ms/u/s!Amz_vh8OYDX3vL5tbUTx6S_W2nnWaA?e=LeCOYI
Recreated, the crucial step appears to be the unchecking of "Run In Background" in the Player Settings.
That's great 👍