Add MusicPlayer & handle NoAudioHardwareException.

This commit is contained in:
Colin McMillen 2020-11-18 11:26:21 -05:00
parent 39ac88b60a
commit a08c1bcfc2
4 changed files with 69 additions and 25 deletions

40
Shared/MusicPlayer.cs Normal file
View File

@ -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();
}
}
}

View File

@ -14,6 +14,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FSM.cs" /> <Compile Include="$(MSBuildThisFileDirectory)FSM.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IScene.cs" /> <Compile Include="$(MSBuildThisFileDirectory)IScene.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IWorld.cs" /> <Compile Include="$(MSBuildThisFileDirectory)IWorld.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MusicPlayer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NPC.cs" /> <Compile Include="$(MSBuildThisFileDirectory)NPC.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ProfilingList.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ProfilingList.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SneakScene.cs" /> <Compile Include="$(MSBuildThisFileDirectory)SneakScene.cs" />

View File

@ -15,7 +15,7 @@ namespace SemiColinGames {
private readonly RenderTarget2D sceneTarget; private readonly RenderTarget2D sceneTarget;
private readonly BasicEffect basicEffect; private readonly BasicEffect basicEffect;
private readonly SpriteBatch spriteBatch; 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 // 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 // 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); spriteBatch = new SpriteBatch(graphics);
musicPlayer = new MusicPlayer();
music = SoundEffects.IntroMusic.CreateInstance(); musicPlayer.Load(SoundEffects.IntroMusic);
music.IsLooped = true;
music.Volume = 0.1f;
} }
~SneakScene() { ~SneakScene() {
@ -48,8 +46,7 @@ namespace SemiColinGames {
} }
public void Dispose() { public void Dispose() {
music.Stop(); musicPlayer.Dispose();
music.Dispose();
sceneTarget.Dispose(); sceneTarget.Dispose();
basicEffect.Dispose(); basicEffect.Dispose();
spriteBatch.Dispose(); spriteBatch.Dispose();
@ -126,9 +123,9 @@ namespace SemiColinGames {
string text = Strings.Paused; string text = Strings.Paused;
Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize); Vector2 position = Textures.BannerFont.CenteredOn(text, camera.HalfSize);
Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White); Text.DrawOutlined(spriteBatch, Textures.BannerFont, text, position, Color.White);
music.Pause(); musicPlayer.Pause();
} else { } else {
music.Play(); musicPlayer.Play();
} }
spriteBatch.End(); spriteBatch.End();

View File

@ -7,7 +7,9 @@ namespace SemiColinGames {
public static SoundEffect IntroMusic; public static SoundEffect IntroMusic;
public static SoundEffect[] SwordSwings = new SoundEffect[14]; public static SoundEffect[] SwordSwings = new SoundEffect[14];
public static void Load(ContentManager content) { // Returns true if all SoundEffects were successfully loaded.
public static bool Load(ContentManager content) {
try {
IntroMusic = content.Load<SoundEffect>("music/playonloop/smash_bros_short"); IntroMusic = content.Load<SoundEffect>("music/playonloop/smash_bros_short");
SwordSwings[0] = content.Load<SoundEffect>("sfx/zs_whoosh_30568"); SwordSwings[0] = content.Load<SoundEffect>("sfx/zs_whoosh_30568");
SwordSwings[1] = content.Load<SoundEffect>("sfx/zs_whoosh_30569"); SwordSwings[1] = content.Load<SoundEffect>("sfx/zs_whoosh_30569");
@ -23,6 +25,10 @@ namespace SemiColinGames {
SwordSwings[11] = content.Load<SoundEffect>("sfx/zs_whoosh_30579"); SwordSwings[11] = content.Load<SoundEffect>("sfx/zs_whoosh_30579");
SwordSwings[12] = content.Load<SoundEffect>("sfx/zs_whoosh_30580"); SwordSwings[12] = content.Load<SoundEffect>("sfx/zs_whoosh_30580");
SwordSwings[13] = content.Load<SoundEffect>("sfx/zs_whoosh_30581"); SwordSwings[13] = content.Load<SoundEffect>("sfx/zs_whoosh_30581");
return true;
} catch (NoAudioHardwareException) {
return false;
}
} }
} }
} }