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 net.minecraft.resources.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
    default CompletableFuture<net.minecraft.client.sounds.AudioStream>
    getAudioStream(net.minecraft.client.sounds.SoundBufferLibrary library, net.minecraft.resources.Identifier id, boolean repeatInstantly)
    Loads the audio stream for this sound.
  • Field Details

  • Method Details

    • getAudioStream

      default CompletableFuture<net.minecraft.client.sounds.AudioStream> getAudioStream(net.minecraft.client.sounds.SoundBufferLibrary library, net.minecraft.resources.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(SoundBufferLibrary, 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(Identifier.fromNamespaceAndPath("modid", "custom_sound"), SoundSource.BLOCKS, SoundInstance.createUnseededRandom());
          }
      
          @Override
          public CompletableFuture<AudioStream> getAudioStream(SoundBufferLibrary library, Identifier id, boolean repeatInstantly) {
              // Return your custom AudioStream implementation.
              return CompletableFuture.completedFuture(new CustomStream());
          }
      }
      
      Parameters:
      library - The default buffer library, 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