Jump to content

[1.8]is there a way to stop a playSoundEffect


frenchtoaster10

Recommended Posts

im trying to make a radio and i want the music to stop when i click again. so how do i stop a sound once it has started to play?

 

 

this is the code i have but how do i stop the sound?

 

@Override

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)

{

worldIn.playSoundEffect(0, 0, 0, "tm:ism", 4, 1);

 

System.out.println("Working1");

 

return true;

}

Link to comment
Share on other sites

Simplest way is to use records instead of sound effects - you can stop a record playing at a given position by passing null as the argument:

player.worldObj.playRecord(blockpos, null);

Note that that code only works on the client side, so if your sound is playing for all nearby players, you will need to send a packet to each of them so they can call that code.

Link to comment
Share on other sites

You need to do the sounds differently, as a streaming source. I'm away from my source code at the moment, or I'd look up how I did it.

 

Long story short, it's routed through my common and client proxies (I'm playing a sound effect for a machine, client already knows the machine is operating due to synced TE data, so it just tells the proxy the start or stop the sound as needed, no networking required).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Here's what I did, which is based on the way minecart sounds work.

 

The client proxy is responsible for playing the actual sound (the server side does nothing) and making sure that we've only got 1 sound playing per location.

The "ChunkCoordTriplet" class is a poorly named object which is effectively a Vec4.  It was originally intended for storing 3D chunk coordinates + dimension, hence the name; overrides equals() and getHashCode() as to be a valid key type.  You could probably just use a BlockPos.

@Override
public void startMillSound(TileEntityMillstone te) {
	ChunkCoordTriplet tepos = new ChunkCoordTriplet(te.getWorldObj().provider.dimensionId, te.xCoord, te.yCoord, te.zCoord);
	if(!sounds.containsKey(tepos)) {
		SoundWindmill snd = new SoundWindmill(new ResourceLocation("ores:grain-mill-loop"), te);

		//this is the important bit
		Minecraft.getMinecraft().getSoundHandler().playSound(snd);
		sounds.put(tepos, snd);
	}
	else {
		SoundWindmill snd = sounds.get(tepos);
		if(snd.isDonePlaying()) {
			sounds.remove(tepos);
			snd = new SoundWindmill(new ResourceLocation("ores:grain-mill-loop"), te);
			Minecraft.getMinecraft().getSoundHandler().playSound(snd);
			sounds.put(tepos, snd);
		}
	}
}

 

This is the class that handles the actual sound:

 

public class SoundWindmill extends PositionedSound implements ITickableSound{
protected TileEntityMillstone millstone;
    protected boolean donePlaying = false;

protected SoundWindmill(ResourceLocation sound, TileEntityMillstone mill) {
	super(sound);
        	this.repeat = true;
        	millstone = mill;
        	this.xPosF = mill.xCoord;
        	this.yPosF = mill.yCoord;
        	this.zPosF = mill.zCoord;
}

public void update() {
	TileEntity te = millstone.getWorldObj().getTileEntity((int)xPosF,(int)yPosF,(int)zPosF);

	//handles checking to make sure the TE is still loaded, not replaced, and checks if the sound still needs to play
	//using the same function I would use in a GUI if this machine had one (its also used by WAILA).
	//For your use you'd probably just need to check the on/off state of the radio and make sure it's synced to the client
	//Metadata/Blockstate might be a good option
	if (!(te != null && te instanceof TileEntityMillstone) || millstone.getGrindTime() <= 0) {
		donePlaying = true;
        	}
    	}

public boolean isDonePlaying() {
        	return donePlaying;
    	}
}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

The nice thing about streaming sounds like records is that you don't have to track individual instances yourself to stop them from playing, just the coordinates. That said, the TickingSound is much more ideal for loops and it's not much more effort to use those, as Draco showed, so it's probably better for your purpose.

 

Note that whichever method you use, you are still going to have to send some data to the other players if you want them to hear your sound, either via the block notifying client players of its current state if you use that to track when sound is playing (which would happen automatically when you change state, so you, personally, wouldn't technically have to do anything), or by actually sending packets each time a player clicks the block.

Link to comment
Share on other sites

Yeah, mine's a looped sound. Was easy enough to create and manage, so the extra work traking individual instances want that much of a bother.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.

Announcements



×
×
  • Create New...

Important Information

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