Hi,
I’ve encountered an issue in the WindowsDX build of MonoGame 3.8, where if I Stop a currently playing SoundEffectInstance and then Play it within the same frame (effectively, restarting it from the start), the sound effect doesn’t play. Only on the next run through the Update() loop can the sound play.
In comparison, under the DesktopGL build, you can Stop() then Play() the sound and it restarts correctly.
Here’s some basic code that replicates the issue.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace SoundBug
{
public class Game1 : Game
{
GraphicsDeviceManager _graphics;
SoundEffectInstance _EffectInstance;
KeyboardState _CurrentKeys;
KeyboardState _PreviousKeys;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
SoundEffect effect = Content.Load<SoundEffect>("Medium Explosion");
_EffectInstance = effect.CreateInstance();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_PreviousKeys = _CurrentKeys;
_CurrentKeys = Keyboard.GetState();
if (_CurrentKeys.IsKeyDown(Keys.F) && _PreviousKeys.IsKeyUp(Keys.F))
{
if (_EffectInstance.State != SoundState.Stopped)
{
_EffectInstance.Stop();
}
_EffectInstance.Play();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}