Sounds - 1.7.10
Playing a sound
The first thing you want to do is acquire a sound file. Basic sounds in Minecraft, like a mob sound, are coded in the .ogg format. Audacity is a good tool that you can use to convert .mp3 and .wav to .ogg.
To make something play a sound, the code is:
world.playSoundAtEntity(player, "modid:myFirstSound", 1.0F, 1.0F);
This requires you to call this line inside a method that has access to the class World or something that can get worldObj, like EntityPlayer.
The first parameter (player) is the entity that you will be playing the sound at. The second ("modid:myFirstSound") is the sound. The third is the volume and the fourth is the pitch.
So if you wanted to play a sound when an item is right clicked you would have:
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
world.playSoundAtEntity(player, "modid:myFirstSound", 1.0F, 1.0F);
return itemStack;
}
Registering a sound
Sound files are now located inside the assets.modid.sounds folder.
Next, create a sounds.json file. A json is a type of text file. Inside here is where all of the sounds properties will go. After making the json, place it inside assets.modid. You do not need to register the file anywhere, it is automatically detected and registered.
To register a sound, you type this:
{
"myFirstSound": {"category": "master","sounds": [{"name": "myFirstSound1","stream": false}, {"name": "myFirstSound2","stream": false}]},
"myFirstRecord": {"category": "master","sounds": [{"name": "myFirstRecord","stream": true}]}
}
Notice how the first line has a comma after it. If you forget this comma no sound below it will load. The only line that won't have a comma is the last sound you add.
The first part ("myFirstSound") is the name you will call when you want to play that sound. "category": "master" is the category, and relates to what slider the user will need to adjust to change the volume of the sound in game. Other categories are "record" or "mob".
"sounds": is the actual sound file. "myFirstSound1" is the ogg the json will try to find, in this case myFirstSound1.ogg. "stream" is used if the sound file is large and would take up a lot of memory if Minecraft preloaded it.
For the first two, whenever you would play "modid:myFirstSound" either myFirstSound1 or myFirstSound2 will play.
That's it!
The first thing you want to do is acquire a sound file. Basic sounds in Minecraft, like a mob sound, are coded in the .ogg format. Audacity is a good tool that you can use to convert .mp3 and .wav to .ogg.
To make something play a sound, the code is:
world.playSoundAtEntity(player, "modid:myFirstSound", 1.0F, 1.0F);
This requires you to call this line inside a method that has access to the class World or something that can get worldObj, like EntityPlayer.
The first parameter (player) is the entity that you will be playing the sound at. The second ("modid:myFirstSound") is the sound. The third is the volume and the fourth is the pitch.
So if you wanted to play a sound when an item is right clicked you would have:
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
world.playSoundAtEntity(player, "modid:myFirstSound", 1.0F, 1.0F);
return itemStack;
}
Registering a sound
Sound files are now located inside the assets.modid.sounds folder.
Next, create a sounds.json file. A json is a type of text file. Inside here is where all of the sounds properties will go. After making the json, place it inside assets.modid. You do not need to register the file anywhere, it is automatically detected and registered.
To register a sound, you type this:
{
"myFirstSound": {"category": "master","sounds": [{"name": "myFirstSound1","stream": false}, {"name": "myFirstSound2","stream": false}]},
"myFirstRecord": {"category": "master","sounds": [{"name": "myFirstRecord","stream": true}]}
}
Notice how the first line has a comma after it. If you forget this comma no sound below it will load. The only line that won't have a comma is the last sound you add.
The first part ("myFirstSound") is the name you will call when you want to play that sound. "category": "master" is the category, and relates to what slider the user will need to adjust to change the volume of the sound in game. Other categories are "record" or "mob".
"sounds": is the actual sound file. "myFirstSound1" is the ogg the json will try to find, in this case myFirstSound1.ogg. "stream" is used if the sound file is large and would take up a lot of memory if Minecraft preloaded it.
For the first two, whenever you would play "modid:myFirstSound" either myFirstSound1 or myFirstSound2 will play.
That's it!