View Javadoc
1   /******************************************************************************
2    * AudioManager.java - Let's make some noise.
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.5  2011/05/10 16:08:20  dick
10   * Fix the javadoc pointers to the source code.
11   *
12   * Revision 1.4  2010/03/03 13:12:21  inim
13   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
14   *
15   * Revision 1.3  2010/03/03 12:19:49  inim
16   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
17   *
18   * Revision 1.2  2010/02/15 09:22:22  dick
19   * Play audio cues when Your opponent doubles or when he drops the match.
20   *
21   * Revision 1.1  2010/02/04 05:57:53  inim
22   * Mavenized project folder layout
23   *
24   * Revision 1.6  2009/02/25 09:41:12  dick
25   * Handle ATTENTION messages from FIBS when it is shutting down.
26   *
27   * Revision 1.5  2009/02/17 14:43:42  dick
28   * Wrap debug with DEBUG.
29   *
30   * Revision 1.4  2009/02/16 06:56:11  dick
31   * Audio works in a jar file now.
32   *
33   * Revision 1.3  2009/02/14 12:04:51  dick
34   * BuckoFIBS is released under the GNU license.
35   * Javadoc.
36   *
37   * Revision 1.2  2009/02/12 06:05:52  dick
38   * Add Cue.CantMoveHere.
39   *
40   * Revision 1.1  2009/02/03 09:43:02  dick
41   * Let's make some noise.
42   *
43   */
44  
45  /* 
46   * This program is free software: you can redistribute it and/or modify
47   * it under the terms of the GNU General Public License as published by
48   * the Free Software Foundation, either version 3 of the License, or
49   * (at your option) any later version.
50   *
51   * This program is distributed in the hope that it will be useful,
52   * but WITHOUT ANY WARRANTY; without even the implied warranty of
53   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54   * GNU General Public License for more details.
55   *
56   * You should have received a copy of the GNU General Public License
57   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
58   *
59   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
60   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
61   * 
62   */
63  package com.buckosoft.fibs.BuckoFIBS;
64  
65  import java.awt.Component;
66  import java.net.URI;
67  import java.net.URL;
68  import java.util.LinkedList;
69  
70  import javax.swing.JOptionPane;
71  
72  import sun.audio.AudioPlayer;
73  import sun.audio.AudioStream;
74  
75  /** Let's make some noise. <br>
76   * Audio hack from <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip24.html">
77   * 	Java Tip 24: How to play audio in applications
78   * </a><br>
79   * This component does not work when launched from jnlp.<br>
80   * @author Dick Balaska
81   * @since 2009/02/02
82   * @version $Revision$ <br> $Date$
83   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/AudioManager.java">cvs AudioManager.java</a>
84   * @see com.buckosoft.fibs.BuckoFIBS.AudioManager.Cue
85   */
86  @SuppressWarnings("restriction")
87  public class AudioManager {
88  	private final static boolean DEBUG = false;
89  
90  	/** Define the Sound events that we can play
91  	 * @author Dick Balaska
92  	 * @since 2009/02/02
93  	 * @version $Revision$ <br> $Date$
94  	 * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/AudioManager.java">cvs AudioManager.java</a>
95  	 */
96  	public enum Cue {
97  		/** You have been invited to play a match with someone. */
98  		Invited,
99  		/** It's your turn to roll or double */
100 		RollOrDouble,
101 		/** It's your turn. */
102 		YourTurn,
103 		/** You pick up the dice and end your turn. */
104 		PickUpDice,
105 		/** A chat message has been displayed in the chat window. */
106 		ChatReceived,
107 		/** You tried to move a checker to an occupied space. */
108 		CantMoveHere,
109 		/** FIBS wants your attention, like before a shutdown */
110 		FibsAttention,
111 		/** Opponent has aborted the match */
112 		MatchAborted,
113 		/** Opponent has doubled, You should accept or decline */
114 		Doubled,
115 		
116 	}
117 
118 	private class CueAndFile {
119 		CueAndFile(Cue c, URI u, String s) {
120 			this.cue = c;
121 			this.uri = u;
122 			this.s = s;
123 		}
124 		Cue	cue;
125 		URI	uri;
126 		String s;
127 	}
128 	
129 	private LinkedList<CueAndFile>	list = new LinkedList<CueAndFile>();
130 
131 	/** The default constructor.
132 	 * Don't do anything useful because we want to catch the exceptions via the initialize method.
133 	 */
134 	public AudioManager() {}
135 	
136 	/** Initialize the AudioManager.
137 	 * @param parent Parent for any dialog boxes that may need to pop open
138 	 */
139 	public void initialize(Component parent) {
140 		_init(parent, Cue.Invited,		"ringin");
141 		_init(parent, Cue.RollOrDouble,	"waiting");
142 		_init(parent, Cue.YourTurn,		"yourTurn");
143 		_init(parent, Cue.PickUpDice,	"pickUpDice");
144 		_init(parent, Cue.ChatReceived,	"chatReceived");
145 		_init(parent, Cue.CantMoveHere,	"failure");
146 		_init(parent, Cue.FibsAttention,"failure");
147 		_init(parent, Cue.MatchAborted,	"aborted");
148 		_init(parent, Cue.Doubled,		"doubled");
149 	}
150 	
151 	private	void _init(Component parent, Cue cue, String fname) {
152 		URL url;
153 		URI uri;
154 		String s = "/audio/" + fname + ".wav";
155 		url = getClass().getResource(s);
156 		if (url == null) {
157 			JOptionPane.showMessageDialog(parent, "Can't find audio file '" + s + "'",
158 					"Warning", JOptionPane.WARNING_MESSAGE);
159 			return;
160 		}
161 		try {
162 			uri = url.toURI();
163 		} catch (Exception ex) {
164 			ex.printStackTrace();
165 			JOptionPane.showMessageDialog(parent, "Can't load audio file '" + s + "'",
166 					"Warning", JOptionPane.WARNING_MESSAGE);
167 			return;
168 		}
169 		list.add(new CueAndFile(cue, uri, s));
170 	}
171 
172 	/** Play this event
173 	 * @param cue The event to play
174 	 */
175 	@SuppressWarnings("restriction")
176 	public void play(Cue cue) {
177 		try {
178 			for (CueAndFile caf : list) {
179 				if (caf.cue == cue) {
180 					if (DEBUG)
181 						System.out.println("uri=" + caf.uri.toString());
182 					AudioStream as = new AudioStream(getClass().getResourceAsStream(caf.s));
183 					AudioPlayer.player.start(as);            
184 					return;
185 				}
186 			}
187 		} catch (Exception ex) {
188 			ex.printStackTrace();
189 		}
190 	}
191 }