package org.teachingextensions.logo;

import java.awt.Toolkit;
import java.io.File;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

/**
 * <img src="http://www.spellzone.com/images/sound-icon.gif" style="text-align: left" alt="A speaker with sound waves" >
 * Sound allows you to play a sound, like a 'beep' or the sound from a file 
 */
public class Sound
{
  /**
   * Plays a beep through your speakers. BEEP!<br>
   * <b>Example:</b> {@code  Sound.playBeep()}
   */
  public static void playBeep()
  {
    Toolkit.getDefaultToolkit().beep();
  }
  /**
   * Check out our cool sounds - thanks to http://www.wavsource.com/
   */
  public enum TKPSound {
    Ahem, Applause, BrainIsGone, CatMeow, Cheering, Crickets, DoctorMccoy, Fanfare, FargoYah, Gong, LLCoolJYaKnow,
    LosingPower, Malfunction, Pizza, RunAway, SheerIgnorance, SoBeIt, StrangePerson, Stubborn, Yahoo, Yay
  }
  private String soundFilename = null;
  public synchronized void setSound(TKPSound mySound)
  {
    String sound = "soundFiles/" + mySound + ".wav";
    URL resource = this.getClass().getResource(sound);
    if (resource == null)
    {
      resource = this.getClass().getClassLoader().getResource(sound);
    }
    if (resource == null) { throw new IllegalStateException("Could not get TKPSound: " + sound); }
    this.soundFilename = resource.toString();
    this.soundFilename = this.soundFilename.replace("file:", "");
  }
  /**
   * Plays a sound through your speakers. Use a '.wav' file<br>
   * <b>Example:</b> {@code  Sound.playSound("mySound.wav")}
   */
  public synchronized void playSound()
  {
    final String sound = this.soundFilename;
    new Thread(new Runnable()
    {
      @Override
      public void run()
      {
        try
        {
          Clip clip = AudioSystem.getClip();
          AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(sound));
          clip.open(inputStream);
          clip.start();
        }
        catch (Exception e)
        {
          System.out.println("play sound error: " + e.getMessage() + " for " + sound);
        }
      }
    }).start();
  }
}

@@ -60,29 +60,8 @@
 it;%0A
-import java.io.File;%0A
 impo
@@ -77,16 +77,16 @@
 et.URL;%0A
+
 %0Aimport 
@@ -959,28 +959,20 @@
 ate 
-String soundFilename
+URL soundUrl
  = n
@@ -1363,24 +1363,19 @@
 is.sound
-Filename
+Url
  = resou
@@ -1381,85 +1381,8 @@
 urce
-.toString();%0A    this.soundFilename = this.soundFilename.replace(%22file:%22, %22%22)
 ;%0A  
@@ -1573,22 +1573,19 @@
   final 
-String
+URL
  sound =
@@ -1599,16 +1599,11 @@
 ound
-Filename
+Url
 ;%0A  
@@ -1828,24 +1828,14 @@
 eam(
-new File(
 sound)
-)
 ;%0A  

Use URL overload for Sound