This example show you how you add new sounds to your MOD and play them. First of all you need a sound file which has the format *.ogg. Any other format is not allowed by the Minecraft application and will be rejected.
The soundfile has the name: sound1.ogg
Put the sound file under the following path:
/YourPath/src/main/resources/assets/MODID/sounds/sound1.ogg
Replace 'MODID' by the identifier you defined for your MOD
Next you have to create a sounds.json in UTF-8 (Standard) encoding which defines name, resource,... and other things for your custom sound. This file will look like:
{
"sound1": {
"category" : "player",
"sounds": [{
"name": "MODID:sound1",
"stream": false
}
]
},
"sound2": {
"category" : "ambient",
"sounds": [{
"name": "MODID:subfolder/sound2",
"stream": true
}
]
}
}
As explanation for this sounds.json.
There are defined 2 sounds defined, as I added an example that you can investigate how to add multiply sounds. sound1 has the category player the second is of category ambient which means the volume of the sound is affected by the volume settings the user has set for player/ambient sounds. name is the most important property as it is pointing to the resource of the sound. The MODID is the identifier of your MOD and is mandatory as the Application will search in the resources of your mod for the file, otherwise it will search in the Minecraft resources and will find nothing. The stream property means that the sound will be streamed from file system, which is only needed for sounds longer than 4 seconds.
Your custom sounds.json file has to go under the following path:
/YourPath/src/main/resources/assets/MODID/sounds.json
Now you will be able to load the sounds to the registry of the game. So you have to create a class which is initializing SoundEvents and handling the registration.
public class SoundRegistrator {
public static final SoundEvent SOUND_1;
public static final SoundEvent SOUND_2;
static {
SOUND_1 = addSoundsToRegistry("sound1");
SOUND_2 = addSoundsToRegistry("sound2");
}
private static SoundEvent addSoundsToRegistry(String soundId) {
ResourceLocation shotSoundLocation = new ResourceLocation("MODID", soundId);
SoundEvent soundEvent = new SoundEvent(shotSoundLocation);
soundEvent.setRegistryName(shotSoundLocation);
return soundEvent;
}
}
Afterward you have to create a SoundRegisterListener:
public class SoundRegisterListener {
@SubscribeEvent(priority = EventPriority.NORMAL, receiveCanceled = true)
public void registerSoundEvents(RegistryEvent.Register<SoundEvent> event) {
event.getRegistry().registerAll(SoundRegistrator.SOUND_1,SoundRegistrator.SOUND_2);
}
}
and register it to the MinecraftForge.EVENT_BUS like:
MinecraftForge.EVENT_BUS.register(new SoundRegisterListener());
Finally you will be able to play your sounds:
void playSound(SoundEvent sound) {
try {
if (Minecraft.getMinecraft().world.isRemote) {
EntityPlayerSP player = Minecraft.getMinecraft().player;
Minecraft.getMinecraft().world.playSound(player, player.getPosition(), sound, SoundCategory.PLAYERS, RandomGenerator.getNextRandomVolumeLoud(), 1.0F);
}
} catch (Exception ex) {
//Error happened
}
}