Package greenfoot

Class GreenfootSound

java.lang.Object
greenfoot.GreenfootSound

public class GreenfootSound extends Object
Represents audio that can be played in Greenfoot. A GreenfootSound loads the audio from a file. The sound cannot be played several times simultaneously, but can be played several times sequentially.

Most files of the following formats are supported: AIFF, AU, WAV, MP3 and MIDI.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new sound from the given file.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Get the current volume of the sound, between 0 (off) and 100 (loudest.)
    boolean
    True if the sound is currently playing.
    void
    Pauses the current sound if it is currently playing.
    void
    Start playing this sound.
    void
    Play this sound repeatedly in a loop.
    void
    setVolume(int level)
    Set the current volume of the sound between 0 (off) and 100 (loudest.)
    void
    Stop playing this sound if it is currently playing.
    Returns a string representation of this sound containing the name of the file and whether it is currently playing or not.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • GreenfootSound

      public GreenfootSound(String filename)
      Creates a new sound from the given file.
      Parameters:
      filename - Typically the name of a file in the sounds directory in the project directory.
  • Method Details

    • play

      public void play()
      Start playing this sound. If it is playing already, it will do nothing. If the sound is currently looping, it will finish the current loop and stop. If the sound is currently paused, it will resume playback from the point where it was paused. The sound will be played once.
    • playLoop

      public void playLoop()
      Play this sound repeatedly in a loop. If called on an already looping sound, it will do nothing. If the sound is already playing once, it will start looping instead. If the sound is currently paused, it will resume playing from the point where it was paused.
    • stop

      public void stop()
      Stop playing this sound if it is currently playing. If the sound is played again later, it will start playing from the beginning. If the sound is currently paused it will now be stopped instead.
    • pause

      public void pause()
      Pauses the current sound if it is currently playing. If the sound is played again later, it will resume from the point where it was paused.

      Make sure that this is really the method you want. If possible, you should always use stop(), because the resources can be released after calling stop(). The resources for the sound will not be released while it is paused.

      See Also:
    • isPlaying

      public boolean isPlaying()
      True if the sound is currently playing.
      Returns:
      Whether the sound is currently playing.
    • getVolume

      public int getVolume()
      Get the current volume of the sound, between 0 (off) and 100 (loudest.)
      Returns:
      A number between 0-100 represents the current sound volume.
    • setVolume

      public void setVolume(int level)
      Set the current volume of the sound between 0 (off) and 100 (loudest.)
      Parameters:
      level - the level to set the sound volume to.
    • toString

      public String toString()
      Returns a string representation of this sound containing the name of the file and whether it is currently playing or not.
      Overrides:
      toString in class Object