From a08c1bcfc298861ec0ec1d88ab0c98c2cc0e3c95 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 18 Nov 2020 11:26:21 -0500 Subject: [PATCH] Add MusicPlayer & handle NoAudioHardwareException. --- Shared/MusicPlayer.cs | 40 ++++++++++++++++++++++++++++++++++++++++ Shared/Shared.projitems | 1 + Shared/SneakScene.cs | 15 ++++++--------- Shared/SoundEffects.cs | 38 ++++++++++++++++++++++---------------- 4 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 Shared/MusicPlayer.cs diff --git a/Shared/MusicPlayer.cs b/Shared/MusicPlayer.cs new file mode 100644 index 0000000..c17e1d5 --- /dev/null +++ b/Shared/MusicPlayer.cs @@ -0,0 +1,40 @@ +using Microsoft.Xna.Framework.Audio; +using System; + +namespace SemiColinGames { + public class MusicPlayer : IDisposable { + + private SoundEffectInstance music; + + ~MusicPlayer() { + Dispose(); + } + + public void Dispose() { + Stop(); + music?.Dispose(); + GC.SuppressFinalize(this); + } + + public void Load(SoundEffect sound) { + if (sound == null) { + return; + } + music = sound.CreateInstance(); + music.IsLooped = true; + music.Volume = 0.1f; + } + + public void Stop() { + music?.Stop(); + } + + public void Pause() { + music?.Pause(); + } + + public void Play() { + music?.Play(); + } + } +} diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index c9f32c2..83545d6 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -14,6 +14,7 @@ + diff --git a/Shared/SneakScene.cs b/Shared/SneakScene.cs index d42e720..ab97d84 100644 --- a/Shared/SneakScene.cs +++ b/Shared/SneakScene.cs @@ -15,7 +15,7 @@ namespace SemiColinGames { private readonly RenderTarget2D sceneTarget; private readonly BasicEffect basicEffect; private readonly SpriteBatch spriteBatch; - private readonly SoundEffectInstance music; + private readonly MusicPlayer musicPlayer; // Draw() needs to be called without IsRunningSlowly this many times before we actually // attempt to draw the scene. This is a workaround for the fact that otherwise the first few @@ -37,10 +37,8 @@ namespace SemiColinGames { }; spriteBatch = new SpriteBatch(graphics); - - music = SoundEffects.IntroMusic.CreateInstance(); - music.IsLooped = true; - music.Volume = 0.1f; + musicPlayer = new MusicPlayer(); + musicPlayer.Load(SoundEffects.IntroMusic); } ~SneakScene() { @@ -48,8 +46,7 @@ namespace SemiColinGames { } public void Dispose() { - music.Stop(); - music.Dispose(); + musicPlayer.Dispose(); sceneTarget.Dispose(); basicEffect.Dispose(); spriteBatch.Dispose(); @@ -126,9 +123,9 @@ namespace SemiColinGames { string text = Strings.Paused; Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize); Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White); - music.Pause(); + musicPlayer.Pause(); } else { - music.Play(); + musicPlayer.Play(); } spriteBatch.End(); diff --git a/Shared/SoundEffects.cs b/Shared/SoundEffects.cs index 02d6520..0fda2ba 100644 --- a/Shared/SoundEffects.cs +++ b/Shared/SoundEffects.cs @@ -7,22 +7,28 @@ namespace SemiColinGames { public static SoundEffect IntroMusic; public static SoundEffect[] SwordSwings = new SoundEffect[14]; - public static void Load(ContentManager content) { - IntroMusic = content.Load("music/playonloop/smash_bros_short"); - SwordSwings[0] = content.Load("sfx/zs_whoosh_30568"); - SwordSwings[1] = content.Load("sfx/zs_whoosh_30569"); - SwordSwings[2] = content.Load("sfx/zs_whoosh_30570"); - SwordSwings[3] = content.Load("sfx/zs_whoosh_30571"); - SwordSwings[4] = content.Load("sfx/zs_whoosh_30572"); - SwordSwings[5] = content.Load("sfx/zs_whoosh_30573"); - SwordSwings[6] = content.Load("sfx/zs_whoosh_30574"); - SwordSwings[7] = content.Load("sfx/zs_whoosh_30575"); - SwordSwings[8] = content.Load("sfx/zs_whoosh_30576"); - SwordSwings[9] = content.Load("sfx/zs_whoosh_30577"); - SwordSwings[10] = content.Load("sfx/zs_whoosh_30578"); - SwordSwings[11] = content.Load("sfx/zs_whoosh_30579"); - SwordSwings[12] = content.Load("sfx/zs_whoosh_30580"); - SwordSwings[13] = content.Load("sfx/zs_whoosh_30581"); + // Returns true if all SoundEffects were successfully loaded. + public static bool Load(ContentManager content) { + try { + IntroMusic = content.Load("music/playonloop/smash_bros_short"); + SwordSwings[0] = content.Load("sfx/zs_whoosh_30568"); + SwordSwings[1] = content.Load("sfx/zs_whoosh_30569"); + SwordSwings[2] = content.Load("sfx/zs_whoosh_30570"); + SwordSwings[3] = content.Load("sfx/zs_whoosh_30571"); + SwordSwings[4] = content.Load("sfx/zs_whoosh_30572"); + SwordSwings[5] = content.Load("sfx/zs_whoosh_30573"); + SwordSwings[6] = content.Load("sfx/zs_whoosh_30574"); + SwordSwings[7] = content.Load("sfx/zs_whoosh_30575"); + SwordSwings[8] = content.Load("sfx/zs_whoosh_30576"); + SwordSwings[9] = content.Load("sfx/zs_whoosh_30577"); + SwordSwings[10] = content.Load("sfx/zs_whoosh_30578"); + SwordSwings[11] = content.Load("sfx/zs_whoosh_30579"); + SwordSwings[12] = content.Load("sfx/zs_whoosh_30580"); + SwordSwings[13] = content.Load("sfx/zs_whoosh_30581"); + return true; + } catch (NoAudioHardwareException) { + return false; + } } } }