Add MusicPlayer & handle NoAudioHardwareException.
This commit is contained in:
parent
39ac88b60a
commit
a08c1bcfc2
40
Shared/MusicPlayer.cs
Normal file
40
Shared/MusicPlayer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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,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();
|
||||
|
||||
|
@ -7,7 +7,9 @@ namespace SemiColinGames {
|
||||
public static SoundEffect IntroMusic;
|
||||
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");
|
||||
SwordSwings[0] = content.Load<SoundEffect>("sfx/zs_whoosh_30568");
|
||||
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[12] = content.Load<SoundEffect>("sfx/zs_whoosh_30580");
|
||||
SwordSwings[13] = content.Load<SoundEffect>("sfx/zs_whoosh_30581");
|
||||
return true;
|
||||
} catch (NoAudioHardwareException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user