/**
* Makes sounds, mostly verbal error messages.
*/
package com.mindprod.vercheck;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
/**
* Plays a variety of sounds
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 Created by IntelliJ IDEA.
*/
public enum Sound
{
BAD_DATE( "baddate.au", 6278 ),
BAD_URL( "badurl.au", 6203 ),
CLICK( "click.au", 64 ),
FUTURE_DATE( "futuredate.au", 2412 ),
INVALID_REGEX( "invalidregex.au", 1042 ),
NEW_VERSION( "newversion.au", 909 ),
UNABLE_TO_CONNECT( "unabletoconnect.au", 1161 );
/**
* time safe to make next sound
*/
@SuppressWarnings( { "WeakerAccess" } )
static long nextSound;
/**
* audioclip to play this sound
*/
private AudioClip clip;
/**
* name of this sound file in the jar
*/
private final String resource;
/**
* duration of the sound in milliseconds
*/
private final int duration;
/**
* Convert resources into AudioClips, ready to play.
*
* @param applet apply that will be playing the sounds.
*/
public static void init( Applet applet )
{
try
{
if ( applet.getAppletContext() == null )
{
return;
}
}
catch ( NullPointerException e )
{
return;
}
for ( Sound sound : values() )
{
final URL url = VerCheck.class.getResource( "sound/" + sound.resource );
sound.clip = applet.getAudioClip( url );
}
}
/**
* play sound matching this enum
*/
@SuppressWarnings( { "EmptyCatchBlock" } )
public void play()
{
if ( clip != null )
{
final long now = System.currentTimeMillis();
if ( now < nextSound )
{
try
{
Thread.sleep( nextSound - now );
}
catch ( InterruptedException e )
{
}
}
clip.play();
nextSound = now + duration;
Thread.yield();
}
}
/**
* constructor
*
* @param resource name of resource sound file
* @param duration time in ms to play the sound.
*/
Sound( String resource, int duration )
{
this.resource = resource;
this.duration = duration;
}
}