Class AudioPlayer

  • All Implemented Interfaces:
    java.lang.Runnable, AudioListener

    public final class AudioPlayer
    extends java.lang.Thread
    implements AudioListener
    Creates and controls a separate audio player thread.
    Since:
    12326 (move to new package), 547
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AudioPlayer.Command
      Audio player command.
      class  AudioPlayer.Execute
      Passes information from the control thread to the playing thread
      static class  AudioPlayer.Result
      Audio player result.
      static class  AudioPlayer.State
      Audio player state.
      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.UncaughtExceptionHandler
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private AudioPlayer()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static AudioPlayer getInstance()
      Returns the singleton object, and if this is the first time, creates it along with the thread to support audio
      static java.lang.Class<? extends SoundPlayer> getSoundPlayerClass()
      Returns the custom sound player class, if any.
      static void pause()
      Pauses the currently playing audio stream.
      static boolean paused()
      Whether or not we are paused.
      static void play​(java.net.URL url)
      Plays a WAV audio file from the beginning.
      static void play​(java.net.URL url, double seconds)
      Plays a WAV audio file from a specified position.
      static void play​(java.net.URL url, double seconds, double speed)
      Plays a WAV audio file from a specified position at variable speed.
      static boolean playing()
      Whether or not we are playing.
      void playing​(java.net.URL playingUrl)
      Called when a new URL is being played.
      static double position()
      How far we are through playing, in seconds.
      static void reset()
      Resets the audio player.
      void run()
      Starts the thread to actually play the audio, per Thread interface Not to be used as public, though Thread interface doesn't allow it to be made private
      static void setSoundPlayerClass​(java.lang.Class<? extends SoundPlayer> playerClass)
      Sets the custom sound player class to override default core player.
      static double speed()
      Speed at which we will play.
      static java.net.URL url()
      To get the Url of the playing or recently played audio.
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • play

        public static void play​(java.net.URL url)
                         throws java.lang.InterruptedException,
                                java.io.IOException
        Plays a WAV audio file from the beginning. See also the variant which doesn't start at the beginning of the stream
        Parameters:
        url - The resource to play, which must be a WAV file or stream
        Throws:
        java.lang.InterruptedException - thread interrupted
        java.io.IOException - audio fault exception, e.g. can't open stream, unhandleable audio format
      • play

        public static void play​(java.net.URL url,
                                double seconds)
                         throws java.lang.InterruptedException,
                                java.io.IOException
        Plays a WAV audio file from a specified position.
        Parameters:
        url - The resource to play, which must be a WAV file or stream
        seconds - The number of seconds into the audio to start playing
        Throws:
        java.lang.InterruptedException - thread interrupted
        java.io.IOException - audio fault exception, e.g. can't open stream, unhandleable audio format
      • play

        public static void play​(java.net.URL url,
                                double seconds,
                                double speed)
                         throws java.lang.InterruptedException,
                                java.io.IOException
        Plays a WAV audio file from a specified position at variable speed.
        Parameters:
        url - The resource to play, which must be a WAV file or stream
        seconds - The number of seconds into the audio to start playing
        speed - Rate at which audio plays (1.0 = real time, > 1 is faster)
        Throws:
        java.lang.InterruptedException - thread interrupted
        java.io.IOException - audio fault exception, e.g. can't open stream, unhandleable audio format
      • pause

        public static void pause()
                          throws java.lang.InterruptedException,
                                 java.io.IOException
        Pauses the currently playing audio stream. Does nothing if nothing playing.
        Throws:
        java.lang.InterruptedException - thread interrupted
        java.io.IOException - audio fault exception, e.g. can't open stream, unhandleable audio format
      • url

        public static java.net.URL url()
        To get the Url of the playing or recently played audio.
        Returns:
        url - could be null
      • paused

        public static boolean paused()
        Whether or not we are paused.
        Returns:
        boolean whether or not paused
      • playing

        public static boolean playing()
        Whether or not we are playing.
        Returns:
        boolean whether or not playing
      • position

        public static double position()
        How far we are through playing, in seconds.
        Returns:
        double seconds
      • speed

        public static double speed()
        Speed at which we will play.
        Returns:
        double, speed multiplier
      • getInstance

        private static AudioPlayer getInstance()
        Returns the singleton object, and if this is the first time, creates it along with the thread to support audio
        Returns:
        the unique instance
      • reset

        public static void reset()
        Resets the audio player.
      • run

        public void run()
        Starts the thread to actually play the audio, per Thread interface Not to be used as public, though Thread interface doesn't allow it to be made private
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • playing

        public void playing​(java.net.URL playingUrl)
        Description copied from interface: AudioListener
        Called when a new URL is being played.
        Specified by:
        playing in interface AudioListener
        Parameters:
        playingUrl - new URL being played
      • getSoundPlayerClass

        public static java.lang.Class<? extends SoundPlayergetSoundPlayerClass()
        Returns the custom sound player class, if any.
        Returns:
        the custom sound player class, or null
        Since:
        14183
      • setSoundPlayerClass

        public static void setSoundPlayerClass​(java.lang.Class<? extends SoundPlayer> playerClass)
        Sets the custom sound player class to override default core player. Must be called before the first audio method invocation.
        Parameters:
        playerClass - custom sound player class to override default core player
        Since:
        14183