Interface FabricSoundInstance


public interface FabricSoundInstance
General purpose Fabric-provided extensions to SoundInstance.

This interface is implicitly implemented on all SoundInstances via a mixin and interface injection.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Identifier
    An empty sound, which may be used as a placeholder in your sounds.json file for sounds with custom audio streams.
  • Method Summary

    Modifier and Type
    Method
    Description
    getAudioStream(SoundLoader loader, Identifier id, boolean repeatInstantly)
    Loads the audio stream for this sound.
  • Field Details

  • Method Details

    • getAudioStream

      default CompletableFuture<AudioStream> getAudioStream(SoundLoader loader, Identifier id, boolean repeatInstantly)
      Loads the audio stream for this sound.

      By default this will load .ogg files from active resource packs. It may be overridden to provide a custom AudioStream implementation which provides audio from another source, such as over the network or driven from user input.

      Usage Example

      Creating a sound with a custom audio stream requires the following:

      Firstly, an entry in sounds.json. The name can be set to any sound (though it is recommended to use the dummy EMPTY_SOUND), and the "stream" property set to true:

      
       {
         "custom_sound": {"sounds": [{"name": "fabric-sound-api-v1:empty", "stream": true}]}
       }
       

      You should then define your own implementation of AudioStream, which provides audio data to the sound engine.

      Finally, you'll need an implementation of SoundInstance which overrides getAudioStream(net.minecraft.client.sound.SoundLoader, net.minecraft.util.Identifier, boolean) to return your custom implementation. SoundInstance.getSound() should return the newly-added entry in sounds.json.

      
       class CustomSound extends AbstractSoundInstance {
           CustomSound() {
               // Use the sound defined in sounds.json
               super(new Identifier("mod_id", "custom_sound"), SoundCategory.BLOCKS, SoundInstance.createRandom());
           }
      
           @Override
           public CompletableFuture<AudioStream> getAudioStream(SoundLoader loader, Identifier id, boolean repeatInstantly) {
               // Return your custom AudioStream implementation.
               return CompletableFuture.completedFuture(new CustomStream());
           }
       }
       
      Parameters:
      loader - The default sound loader, capable of loading .ogg files.
      id - The resolved sound ID, equal to SoundInstance.getSound()'s location.
      repeatInstantly - Whether this sound should loop. This is true when the sound is repeatable and has no delay.
      Returns:
      the loaded audio stream