Jump to content

Checking if sound is playing && ISound[srsly noone?]


Lua

Recommended Posts

Hi, Im adding background music but Ive ran into a problem making sure no other music is playing before I play mine.

 

Heres what I got

 

@SubscribeEvent
public void playMyMusic(ClientTickEvent event)
{
	Random rand = new Random();
	ISound soundCache = null;
	SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
	int i = rand.nextInt(1000);
	if(soundCache == null || !handler.isSoundPlaying(soundCache))
	{
		if(i == 0)
		{
			System.out.println("playing sound");
			soundCache = new PositionedSoundRecord(new ResourceLocation(Arcticraft.MOD_ID + ":records.frozen_feelings"), 1.0F, 1.0F, 1.0F, 1.0F, 1.0F);
			handler.playSound(soundCache);
		}
	}
}

 

My sound plays but it will play over the top of any currently playing which I dont want. Obviously, its playing because soundCache does = null but thats because I have no idea how ISound works and didnt get any google results.

Link to comment
Share on other sites

Okay, there is the Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(ISound), stopSounds() and stopSound(ISound) methods available.

 

The stopSounds() will stop all sounds.

 

To stop a specific sound, you'd need to have the instance of the ISound to pass to the stopSound() method, which you probably won't have if it is a vanilla sound. But for your own sounds you can stop them this way.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

So it's just not possible to add your own music properly, you can't check if vanilla songs are playing, so you always run the risk of playing your music over the vanilla tracks.

 

Unless you turn off all sounds yes.

 

However, you might be able to use reflection. The sound handler has a private field for the sound manager which has a private field for playingSounds map from which you could iterate through and check the ISound returned to find the private resource location.

 

I'm not that strong in reflection but I think that is possible.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Ok, this post was getting messy, there's no reason to show the totally incorrect things I tried, so here's what works:

 

 

SoundManager mng = ReflectionHelper.getPrivateValue(SoundHandler.class, Minecraft.getMinecraft().getSoundHandler(), "sndManager");
Map playingSounds = ReflectionHelper.getPrivateValue(SoundManager.class, mng, "playingSounds");
Iterator it = playingSounds.keySet().iterator();
while(it.hasNext())
{
   PositionedSound ps = (PositionedSound)playingSounds.get(it.next());
   ResourceLocation reloc = ReflectionHelper.getPrivateValue(PositionedSound.class, ps, "field_147664_a");
   if("music.game".equals(reloc.getResourcePath()))
   {
      Minecraft.getMinecraft().getSoundHandler().stopSound(ps);
      System.out.println("stopped music");
      break;
   }
}

Link to comment
Share on other sites

Ok, this post was getting messy, there's no reason to show the totally incorrect things I tried, so here's what works:

 

 

SoundManager mng = ReflectionHelper.getPrivateValue(SoundHandler.class, Minecraft.getMinecraft().getSoundHandler(), "sndManager");
Map playingSounds = ReflectionHelper.getPrivateValue(SoundManager.class, mng, "playingSounds");
Iterator it = playingSounds.keySet().iterator();
while(it.hasNext())
{
   PositionedSound ps = (PositionedSound)playingSounds.get(it.next());
   ResourceLocation reloc = ReflectionHelper.getPrivateValue(PositionedSound.class, ps, "field_147664_a");
   if("music.game".equals(reloc.getResourcePath()))
   {
      Minecraft.getMinecraft().getSoundHandler().stopSound(ps);
      System.out.println("stopped music");
      break;
   }
}

This will crash in obf environment. When passing field name to reflection helper, pass both obfuscated and not obf names, reflection helper will not convert them into obfuscated fields...

Link to comment
Share on other sites

So, how do you get the obfuscated names?

Or you can use ObfuscationReflectionHelper, that will convert them automatically, or you go to user\.gradle\caches\minecraft\net\minecraftforge\forge\[forge version]\unpacked\conf and search in files fields.csv and methods.csv. It looks like obfName,deobfname,desccription. So you can find by one name another...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.