Is there a way to test for what sounds are playing to a player?

I want to make an ambient music that plays when hostile mobs are nearby. I already have the appropriate sound event ready in the custom sounds.json of the resourcepack I'm using and now want to repeatedly play it to all players within a 10-block radius of every creeper, skeleton, zombie etc. (using /execute), but only if neither an enemy ambient nor a boss music is playing to that player. While for the boss music I could repeatedly stop the ambient sound event for everyone in the boss area, the ambient sound itself is more of an issue, because I'm pretty sure it would overlap, and if I stopped it every time I relaunch it the music would loop quite oddly. I also want the music to start playing as immediately as possible when a player gets within the radius, and not only at the beginning of the next loop. So is there a way to achieve what I need, and if yes, how does it work?

Oh, almost forgot to mention, I'm still using 1.8 because that's my Forge version, so please take this into account. (No, that doesn't mean I'd be OK with using mods. I have Forge in primarily because of WorldEdit, and want the map to be playable in Vanilla.)


Solution 1:

Solution: Saving the current music progress in a scoreboard.
This example assumes a loop length of 42 seconds, just to have a value. A score value of 0 makes the music start, everything from 1 to 839 prevents the sound from being played and 840 means that the player is ready to hear the music again.

First, do this:

/scoreboard objectives add music dummy

Then put these commands in your game loop:

/execute as @e[type=Zombie] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/execute as @e[type=Skeleton] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/execute as @e[type=Creeper] at @s run scoreboard players set @a[distance=..10,scores={music=840..}] music 0
/playsound someTunes hostile @a[scores={music=0}]
/scoreboard players add @a[scores={music=..839}] music 1

First you reset the ticking "music" score of every player who is near a mob, but only if it has already ticked up to 42 seconds for him.
Then you play the music to every player who just got their score reset.
Then you tick the score up.

Here (archive) is a list of all entity IDs, to help you not forget some of them.