Package greenfoot

Class GreenfootSound


  • public class GreenfootSound
    extends java.lang.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.

    Version:
    2.4
    Author:
    Poul Henriksen
    • Constructor Summary

      Constructors 
      Constructor Description
      GreenfootSound​(java.lang.String filename)
      Creates a new sound from the given file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getVolume()
      Get the current volume of the sound, between 0 (off) and 100 (loudest.)
      boolean isPlaying()
      True if the sound is currently playing.
      void pause()
      Pauses the current sound if it is currently playing.
      void play()
      Start playing this sound.
      void playLoop()
      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()
      Stop playing this sound if it is currently playing.
      java.lang.String toString()
      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 Detail

      • GreenfootSound

        public GreenfootSound​(java.lang.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 Detail

      • 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:
        stop()
      • 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 java.lang.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 java.lang.Object