Class Sound

java.lang.Object
org.openpatch.scratch.internal.Sound

public class Sound extends Object
The Sound class represents a sound object in Scratch using Java's internal audio classes.

The decoded audio of a file is loaded once and shared, but every sound object plays on a line of its own. Ten sprites that all added the same sound therefore sound ten times at once, while a single sprite still only plays its own sound once at a time.

  • Constructor Details

  • Method Details

    • ofNameOrPath

      public static Sound ofNameOrPath(String name, String pathOrBuiltin)
      Creates a sound from either the name of a sound bundled with Scratch for Java or a path to a sound file.

      A string without a file extension is looked up in BuiltinSounds, everything else is treated as a path, so existing code keeps working.

      Parameters:
      name - the name the sound should have
      pathOrBuiltin - a bundled sound name or a path to a sound file
      Returns:
      the sound
    • loadSound

      public static org.openpatch.scratch.internal.Sound.Samples loadSound(String path)
      Decodes a sound file into memory, or returns it if that already happened.

      Only the samples are cached, not a playable line, so the same file can be played by any number of objects at the same time.

      Parameters:
      path - the path to the sound file
      Returns:
      the decoded audio
    • getName

      public String getName()
    • play

      public void play()
      Plays the sound. A single sound object plays one sound at a time, but other objects holding the same sound are unaffected and can play it alongside.
    • pause

      public void pause()
    • stop

      public void stop()
    • isPlaying

      public boolean isPlaying()
    • setVolume

      public void setVolume(double amp)
    • changeVolume

      public void changeVolume(double step)
    • getVolume

      public double getVolume()