Browse Source

Add MusicPlayer & handle NoAudioHardwareException.

main
Colin McMillen 3 years ago
parent
commit
a08c1bcfc2
  1. 40
      Shared/MusicPlayer.cs
  2. 1
      Shared/Shared.projitems
  3. 15
      Shared/SneakScene.cs
  4. 38
      Shared/SoundEffects.cs

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

1
Shared/Shared.projitems

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

15
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();

38
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<SoundEffect>("music/playonloop/smash_bros_short");
SwordSwings[0] = content.Load<SoundEffect>("sfx/zs_whoosh_30568");
SwordSwings[1] = content.Load<SoundEffect>("sfx/zs_whoosh_30569");
SwordSwings[2] = content.Load<SoundEffect>("sfx/zs_whoosh_30570");
SwordSwings[3] = content.Load<SoundEffect>("sfx/zs_whoosh_30571");
SwordSwings[4] = content.Load<SoundEffect>("sfx/zs_whoosh_30572");
SwordSwings[5] = content.Load<SoundEffect>("sfx/zs_whoosh_30573");
SwordSwings[6] = content.Load<SoundEffect>("sfx/zs_whoosh_30574");
SwordSwings[7] = content.Load<SoundEffect>("sfx/zs_whoosh_30575");
SwordSwings[8] = content.Load<SoundEffect>("sfx/zs_whoosh_30576");
SwordSwings[9] = content.Load<SoundEffect>("sfx/zs_whoosh_30577");
SwordSwings[10] = content.Load<SoundEffect>("sfx/zs_whoosh_30578");
SwordSwings[11] = content.Load<SoundEffect>("sfx/zs_whoosh_30579");
SwordSwings[12] = content.Load<SoundEffect>("sfx/zs_whoosh_30580");
SwordSwings[13] = content.Load<SoundEffect>("sfx/zs_whoosh_30581");
// Returns true if all SoundEffects were successfully loaded.
public static bool Load(ContentManager content) {
try {
IntroMusic = content.Load<SoundEffect>("music/playonloop/smash_bros_short");
SwordSwings[0] = content.Load<SoundEffect>("sfx/zs_whoosh_30568");
SwordSwings[1] = content.Load<SoundEffect>("sfx/zs_whoosh_30569");
SwordSwings[2] = content.Load<SoundEffect>("sfx/zs_whoosh_30570");
SwordSwings[3] = content.Load<SoundEffect>("sfx/zs_whoosh_30571");
SwordSwings[4] = content.Load<SoundEffect>("sfx/zs_whoosh_30572");
SwordSwings[5] = content.Load<SoundEffect>("sfx/zs_whoosh_30573");
SwordSwings[6] = content.Load<SoundEffect>("sfx/zs_whoosh_30574");
SwordSwings[7] = content.Load<SoundEffect>("sfx/zs_whoosh_30575");
SwordSwings[8] = content.Load<SoundEffect>("sfx/zs_whoosh_30576");
SwordSwings[9] = content.Load<SoundEffect>("sfx/zs_whoosh_30577");
SwordSwings[10] = content.Load<SoundEffect>("sfx/zs_whoosh_30578");
SwordSwings[11] = content.Load<SoundEffect>("sfx/zs_whoosh_30579");
SwordSwings[12] = content.Load<SoundEffect>("sfx/zs_whoosh_30580");
SwordSwings[13] = content.Load<SoundEffect>("sfx/zs_whoosh_30581");
return true;
} catch (NoAudioHardwareException) {
return false;
}
}
}
}
Loading…
Cancel
Save