Skip to content

Commit cc6d874

Browse files
committed
added the ability to play a Sound (.wav file)
1 parent 7d01650 commit cc6d874

4 files changed

Lines changed: 53 additions & 23 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.teachingextensions.logo.utils;
2+
3+
import java.awt.Toolkit;
4+
import java.io.File;
5+
6+
import javax.sound.sampled.AudioInputStream;
7+
import javax.sound.sampled.AudioSystem;
8+
import javax.sound.sampled.Clip;
9+
10+
/**
11+
* <img src="http://www.spellzone.com/images/sound-icon.gif" style="text-align: left" alt="A speaker with sound waves" >
12+
* Sound allows you to play a sound, like a 'beep' or the sound from a file
13+
*/
14+
public class Sound
15+
{
16+
/**
17+
* Plays a beep through your speakers. BEEP!<br>
18+
* <b>Example:</b> {@code Sound.playBeep()}
19+
*/
20+
public static void playBeep()
21+
{
22+
Toolkit.getDefaultToolkit().beep();
23+
}
24+
/**
25+
* Plays a sound through your speakers. Use a '.wav' file<br>
26+
* <b>Example:</b> {@code Sound.playSound("mySound.wav")}
27+
*/
28+
public static synchronized void playSound(final String fileName)
29+
{
30+
new Thread(new Runnable()
31+
{
32+
@Override
33+
public void run()
34+
{
35+
try
36+
{
37+
Clip clip = AudioSystem.getClip();
38+
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));
39+
clip.open(inputStream);
40+
clip.start();
41+
}
42+
catch (Exception e)
43+
{
44+
System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
45+
}
46+
}
47+
}).start();
48+
}
49+
}

src/main/java/org/teachingextensions/logo/utils/Sounds.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/org/teachingextensions/simpleparser/Parser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public class Parser
2020
* <div><b>Example:</b><pre>{@code
2121
* Words data = new Words();
2222
* data.action = "Shake";
23-
* data.weapon = "spear";
23+
* data.weapon = "speare";
2424
* String greeting = Parser.parse("Captain {action}{weapon}!!!", data);
2525
* }</pre></div>
2626
*
27-
* Captain Shakespear!!!
27+
* Captain Shakespeare!!!
2828
*
2929
* @param text
3030
* The template with the fields from the data object surrounded in {curlyBraces}

src/test/java/org/teachingextensions/logo/utils/tests/SoundsDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import junit.framework.TestCase;
44

5-
import org.teachingextensions.logo.utils.Sounds;
5+
import org.teachingextensions.logo.utils.Sound;
66

77
public class SoundsDemo extends TestCase
88
{
99
public void testBeep() throws Exception
1010
{
11-
Sounds.playBeep();
11+
Sound.playBeep();
1212
}
1313
}

0 commit comments

Comments
 (0)