-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathSoundManager.java
More file actions
101 lines (83 loc) · 2.49 KB
/
Copy pathSoundManager.java
File metadata and controls
101 lines (83 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/** Manages the sounds for StarLogoBlocks */
public class SoundManager
{
public static boolean DEBUG = false;
//set to true by default
private static boolean enableSound = true;
public static Sound loadSound(String soundFileName)
{
URL url = SoundManager.class.getResource(soundFileName);
if (url == null) {
System.out.println("Could not find resource " + soundFileName);
return null;
}
AudioInputStream audioInputStream;
try
{
audioInputStream = AudioSystem.getAudioInputStream(url);
}
catch(UnsupportedAudioFileException e)
{
e.printStackTrace();
return null;
}
catch(IOException e)
{
e.printStackTrace();
return null;
}
AudioFormat format = audioInputStream.getFormat();
if (DEBUG) {
System.out.println("Loading sound file \"" + url + "\"");
System.out.println("Format = " + format);
}
Clip clip;
try
{
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip)AudioSystem.getLine(info);
clip.open(audioInputStream);
}
catch(LineUnavailableException e)
{
System.out.println("Sound failed to play: System sound is not available.");
return null;
}
catch(IOException e)
{
e.printStackTrace();
return null;
}
return new Sound(clip);
}
/**
* Sets the ability to enable sound within the entire codeblocks library
* If enableSound is set to false, no sounds can be played/heard until
* enableSound is set to true again.
* @param enableSound
*/
public static void setSoundEnabled(boolean enableSound){
SoundManager.enableSound = enableSound;
}
/**
* Returns true iff sounds are being allowed to play.
* @return true iff sounds are being allowed to play.
*/
public static boolean isSoundEnabled(){
return SoundManager.enableSound;
}
}