Class ClipPlayer


  • public class ClipPlayer
    extends java.lang.Object
    A simple audio clip player intended to play audio cues as part of a user interface. The player is intentionally transparent with respect to failure; if a clip fails to load or the system does not have audio support, the player will not throw an exception. This prevents surprise failures on rare systems that do not have audio support, but it also means that these audio cues should never be the only means of providing feedback.

    New clip players are constructed by providing a reference to the audio file that contains the clip to play. This is either a File or a URL. The constructor also specifies whether a clip should be looped. An unlooped clip will play once each time play() is called. A looped clip will continue to play, repeating as necessary, until it is explicitly stopped.

    Since:
    2.1
    Author:
    Chris Jennings
    • Constructor Summary

      Constructors 
      Constructor Description
      ClipPlayer​(java.io.File audioClip, boolean looped)
      Create a ClipPlayer for an audio clip stored in a file.
      ClipPlayer​(java.net.URL audioClip, boolean looped)
      Create a ClipPlayer for a an audio clip stored at a URL.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the clip player, freeing the associated audio system resources.
      boolean isClipValid()
      Returns true if the clip was loaded successfully and the system is able to play it.
      void play()
      Play the audio clip.
      void stop()
      If the clip is currently playing, stop playing it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ClipPlayer

        public ClipPlayer​(java.io.File audioClip,
                          boolean looped)
        Create a ClipPlayer for an audio clip stored in a file. If the clip cannot be read from the file, the player will do nothing when play() is called and isClipValid() will return false.
        Parameters:
        audioClip - the location of the clip to load
        looped - whether to play the clip in a loop
      • ClipPlayer

        public ClipPlayer​(java.net.URL audioClip,
                          boolean looped)
        Create a ClipPlayer for a an audio clip stored at a URL. If the clip cannot be read, the player will do nothing when play() is called and isClipValid() will return false.
        Parameters:
        audioClip - the location of the clip to load
        looped - whether to play the clip in a loop
    • Method Detail

      • play

        public void play()
        Play the audio clip. If set to loop, the clip will continue to play until stop() is called. If the clip is invalid, nothing happens. If the clip is already playing, it will be restarted from the beginning.
      • stop

        public void stop()
        If the clip is currently playing, stop playing it. If the clip is not playing or is invalid, nothing happens.
      • close

        public void close()
        Close the clip player, freeing the associated audio system resources. Subsequent calls to play() will have no effect.
      • isClipValid

        public boolean isClipValid()
        Returns true if the clip was loaded successfully and the system is able to play it.
        Returns:
        true if playing the clip will produce audio