Skip to content

Commit db7477f

Browse files
committed
Update audio controller to use properites for volume and all chapters past it
1 parent b238675 commit db7477f

File tree

6 files changed

+80
-60
lines changed

6 files changed

+80
-60
lines changed

articles/tutorials/building_2d_games/15_audio_controller/index.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ The `AudioController` provides methods to control the state of audio playback in
7777

7878
[!code-csharp[](./snippets/audiocontroller.cs#state)]
7979

80-
#### AudioController Volume Control
81-
82-
The `AudioController` also provides methods to increase and decrease the global volume of songs and sound effects. Add the following methods:
83-
84-
[!code-csharp[](./snippets/audiocontroller.cs#volume)]
85-
8680
#### AudioController IDisposable Implementation
8781

8882
Finally, the `AudioController` implements the `IDisposable` interface. Add the following methods:
@@ -127,7 +121,7 @@ The key changes made here are:
127121

128122
Next, update the `Game1` class to use the audio controller for audio playback. Open *Game1.cs* and make the following updates:
129123

130-
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,101-102,196-197,220-221,274-290)]
124+
[!code-csharp[](./snippets/game1.cs?highlight=45-46,77-78,101-102,196-197,215-216,269-287)]
131125

132126
The key changes made here are:
133127

@@ -137,8 +131,8 @@ The key changes made here are:
137131
4. In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)) the audio manager is used to play the bounce and collect sound effects.
138132
5. In `CheckKeyboardInput` the following checks were added
139133
1. If the M key on the keyboard is pressed, it will toggle mute for all audio.
140-
2. If the + key is pressed, the global volume is increased by `0.1f`.
141-
3. If the - key is pressed, the global volume is decreased by `0.1f`.
134+
2. If the + key is pressed, the song and sound effect volumes are increased by `0.1f`.
135+
3. If the - key is pressed, the song and sound effect volumes are decreased by `0.1f`.
142136

143137
Running the game now will produce the same result as the previous chapter, only now the lifetime of sound effects and the state management of audio is done through the new audio controller. You can also mute and unumte the audio with the M key and increase and decrease the volume using the + and - keys.
144138

articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,64 @@ public class AudioController : IDisposable
2727
/// </summary>
2828
public bool IsMuted { get; private set; }
2929

30+
/// <summary>
31+
/// Gets or Sets the global volume of songs.
32+
/// </summary>
33+
/// <remarks>
34+
/// If IsMuted is true, the getter will always return back 0.0f and the
35+
/// setter will ignore setting the volume.
36+
/// </remarks>
37+
public float SongVolume
38+
{
39+
get
40+
{
41+
if(IsMuted)
42+
{
43+
return 0.0f;
44+
}
45+
46+
return MediaPlayer.Volume;
47+
}
48+
set
49+
{
50+
if(IsMuted)
51+
{
52+
return;
53+
}
54+
55+
MediaPlayer.Volume = Math.Clamp(value, 0.0f, 1.0f);
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Gets or Sets the global volume of sound effects.
61+
/// </summary>
62+
/// <remarks>
63+
/// If IsMuted is true, the getter will always return back 0.0f and the
64+
/// setter will ignore setting the volume.
65+
/// </remarks>
66+
public float SoundEffectVolume
67+
{
68+
get
69+
{
70+
if(IsMuted)
71+
{
72+
return 0.0f;
73+
}
74+
75+
return SoundEffect.MasterVolume;
76+
}
77+
set
78+
{
79+
if(IsMuted)
80+
{
81+
return;
82+
}
83+
84+
SoundEffect.MasterVolume = Math.Clamp(value, 0.0f, 1.0f);
85+
}
86+
}
87+
3088
/// <summary>
3189
/// Gets a value that indicates if this audio controller has been disposed.
3290
/// </summary>
@@ -236,32 +294,4 @@ public void ToggleMute()
236294
}
237295
}
238296
#endregion
239-
240-
#region volume
241-
/// <summary>
242-
/// Increases volume of all audio by the specified amount.
243-
/// </summary>
244-
/// <param name="amount">The amount to increase the audio by.</param>
245-
public void IncreaseVolume(float amount)
246-
{
247-
if (!IsMuted)
248-
{
249-
MediaPlayer.Volume = Math.Min(MediaPlayer.Volume + amount, 1.0f);
250-
SoundEffect.MasterVolume = Math.Min(SoundEffect.MasterVolume + amount, 1.0f);
251-
}
252-
}
253-
254-
/// <summary>
255-
/// Decreases the volume of all audio by the specified amount.
256-
/// </summary>
257-
/// <param name="amount">The amount to decrease the audio by.</param>
258-
public void DecreaseVolume(float amount)
259-
{
260-
if (!IsMuted)
261-
{
262-
MediaPlayer.Volume = Math.Max(MediaPlayer.Volume - amount, 0.0f);
263-
SoundEffect.MasterVolume = Math.Max(SoundEffect.MasterVolume - amount, 0.0f);
264-
}
265-
}
266-
#endregion
267297
}

articles/tutorials/building_2d_games/15_audio_controller/snippets/game1.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,9 @@ protected override void Update(GameTime gameTime)
201201

202202
if (slimeBounds.Intersects(batBounds))
203203
{
204-
// Divide the width and height of the screen into equal columns and
205-
// rows based on the width and height of the bat.
206-
int totalColumns = GraphicsDevice.PresentationParameters.BackBufferWidth / (int)_bat.Width;
207-
int totalRows = GraphicsDevice.PresentationParameters.BackBufferHeight / (int)_bat.Height;
208-
209204
// Choose a random row and column based on the total number of each
210-
int column = Random.Shared.Next(0, totalColumns);
211-
int row = Random.Shared.Next(0, totalRows);
205+
int column = Random.Shared.Next(1, _tilemap.Columns - 1);
206+
int row = Random.Shared.Next(1, _tilemap.Rows - 1);
212207

213208
// Change the bat position by setting the x and y values equal to
214209
// the column and row multiplied by the width and height.
@@ -280,13 +275,15 @@ private void CheckKeyboardInput()
280275
// If the + button is pressed, increase the volume.
281276
if (Input.Keyboard.WasKeyJustPressed(Keys.OemPlus))
282277
{
283-
Audio.IncreaseVolume(0.1f);
278+
Audio.SongVolume += 0.1f;
279+
Audio.SoundEffectVolume += 0.1f;
284280
}
285281

286282
// If the - button was pressed, decrease the volume.
287283
if (Input.Keyboard.WasKeyJustPressed(Keys.OemMinus))
288284
{
289-
Audio.DecreaseVolume(0.1f);
285+
Audio.SongVolume += 0.1f;
286+
Audio.SoundEffectVolume += 0.1f;
290287
}
291288
}
292289

@@ -365,4 +362,4 @@ protected override void Draw(GameTime gameTime)
365362

366363
base.Draw(gameTime);
367364
}
368-
}
365+
}

articles/tutorials/building_2d_games/16_working_with_spritefonts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ The key changes here are:
207207

208208
Finally, open the *Game1.cs* file and make the following changes:
209209

210-
[!code-csharp[](./snippets/game1.cs?highlight=48-58,93-99,126-127,248-249,391-402)]
210+
[!code-csharp[](./snippets/game1.cs?highlight=48-58,93-99,126-127,243-244,388-399)]
211211

212212
The key changes made are:
213213

articles/tutorials/building_2d_games/16_working_with_spritefonts/snippets/game1.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,9 @@ protected override void Update(GameTime gameTime)
226226

227227
if (slimeBounds.Intersects(batBounds))
228228
{
229-
// Divide the width and height of the screen into equal columns and
230-
// rows based on the width and height of the bat.
231-
int totalColumns = GraphicsDevice.PresentationParameters.BackBufferWidth / (int)_bat.Width;
232-
int totalRows = GraphicsDevice.PresentationParameters.BackBufferHeight / (int)_bat.Height;
233-
234229
// Choose a random row and column based on the total number of each
235-
int column = Random.Shared.Next(0, totalColumns);
236-
int row = Random.Shared.Next(0, totalRows);
230+
int column = Random.Shared.Next(1, _tilemap.Columns - 1);
231+
int row = Random.Shared.Next(1, _tilemap.Rows - 1);
237232

238233
// Change the bat position by setting the x and y values equal to
239234
// the column and row multiplied by the width and height.
@@ -308,13 +303,15 @@ private void CheckKeyboardInput()
308303
// If the + button is pressed, increase the volume.
309304
if (Input.Keyboard.WasKeyJustPressed(Keys.OemPlus))
310305
{
311-
Audio.IncreaseVolume(0.1f);
306+
Audio.SongVolume += 0.1f;
307+
Audio.SoundEffectVolume += 0.1f;
312308
}
313309

314310
// If the - button was pressed, decrease the volume.
315311
if (Input.Keyboard.WasKeyJustPressed(Keys.OemMinus))
316312
{
317-
Audio.DecreaseVolume(0.1f);
313+
Audio.SongVolume -= 0.1f;
314+
Audio.SoundEffectVolume -= 0.1f;
318315
}
319316
}
320317

@@ -406,4 +403,4 @@ protected override void Draw(GameTime gameTime)
406403

407404
base.Draw(gameTime);
408405
}
409-
}
406+
}

articles/tutorials/building_2d_games/17_scenes/snippets/gamescene.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,15 @@ private void CheckKeyboardInput()
309309
// If the + button is pressed, increase the volume.
310310
if (keyboard.WasKeyJustPressed(Keys.OemPlus))
311311
{
312-
Core.Audio.IncreaseVolume(0.1f);
312+
Core.Audio.SongVolume += 0.1f;
313+
Core.Audio.SoundEffectVolume += 0.1f;
313314
}
314315

315316
// If the - button was pressed, decrease the volume.
316317
if (keyboard.WasKeyJustPressed(Keys.OemMinus))
317318
{
318-
Core.Audio.DecreaseVolume(0.1f);
319+
Core.Audio.SongVolume -= 0.1f;
320+
Core.Audio.SoundEffectVolume -= 0.1f;
319321
}
320322
}
321323

0 commit comments

Comments
 (0)