Bug - Game crashes if no audio device is enabled (Windows 8.1)

Hey there, so I just spent a long time helping someone try to get the game running and after looking at the event viewer and seeing this output…

Description: The process was terminated due to an unhandled exception.
Exception Info: Microsoft.Xna.Framework.Audio.InstancePlayLimitException
at Microsoft.Xna.Framework.Audio.OpenALSoundController.ReserveSource()
at Microsoft.Xna.Framework.Audio.OggStream…ctor(System.String, System.Action, Int32)
at Microsoft.Xna.Framework.Media.Song.PlatformInitialize(System.String)
at Microsoft.Xna.Framework.Media.Song…ctor(System.String)
Microsoft.Xna.Framework.Content.ContentTypeReader1[[System.__Canon, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Read(Microsoft.Xna.Framework.Content.ContentReader, System.Object) at Microsoft.Xna.Framework.Content.ContentReader.InnerReadObject[[System.__Canon, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.__Canon) at Microsoft.Xna.Framework.Content.ContentReader.ReadObject[[System.__Canon, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]() at Microsoft.Xna.Framework.Content.ContentReader.ReadAsset[[System.__Canon, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]() at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[[System.__Canon, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.String, System.Action1<System.IDisposable>)
mscorlib, Version=, Culture=neutral,

at Engine.ContentHolder.Init(Microsoft.Xna.Framework.Game)
at Engine.GameRoot.LoadContent()
at Microsoft.Xna.Framework.Game.Initialize()
at Engine.GameRoot.Initialize()
at Microsoft.Xna.Framework.Game.DoInitialize()
at Microsoft.Xna.Framework.Game.Run(Microsoft.Xna.Framework.GameRunBehavior)
at MonoGameTiles.Program.RunGame()
at MonoGameTiles.Program.Main(System.String[])

He randomly decided to try enabling an audio device on his computer (apparently he had turned them off for some reason) and magic, no more error.

This is just sort of a quirky little thing, I mean who has their audio disabled when playing games. But still, it’s something to be aware of.

Yeah, you’re probably going to have to use Try-Catch-Finally code blocks everywhere you use audio.

No idea if MonoGame can integrate it into its code base.

Ouch, ok well that’s not a huge deal for me since my game doesn’t have much audio.

I had the same problem with my game.

What I did was that I have my Audio Content Loading inside Try-Catch, if it fails I set NoAudio = true in AudioManager which handles playing all of the game’s Audio.
After loading Content and NoAudio is false, I try to change audio settings inside Try-Catch, if it fails NoAudio = true.

In game if I try to play Sound Effects or Music and NoAudio is true, it simply returns from the functions without playing audio. If user plugs Audio Device in later, the game needs to be restarted.

This is not exclusive to MonoGame, Even GTAV suffers this issue… though instead of crashing it just locks up :stuck_out_tongue:

Really, that’s quite interesting. I mean, I’m sure it’s uncommon for someone to try playing a game with all audio devices disabled.

I’ll keep it in mind for organizing future projects for sure. Though it would still be awesome if it could be fixed inside Monogame.

PR #5547 was just merged earlier today that cleans up OpenAL initialization. Your code still has to catch the NoAudioHardwareException, but you can now handle the case of no audio hardware being found.

1 Like

Sometimes, and probably a rare thing, the display may become disconnected for whatever reason, say Wireless display, and as such not only does the sound disconnect and the video but for however brief or even no devices connected, no audio device will recover as these days if no speaker cable is plugged into the port [regardless of an active speaker at the other end or headphones though if there is a cable plugged in it will use that port but not always the case] sound devices on a desktop will remain off/disabled unless the display has HDMI audio pass through… So since there are so many cases to cater for… The catch event mentioned already should always be implemented…

Hope that made sense… A bit dizzy right now…

Is it possible to plug in audio after the game has already started or does the game need restarted?

The game would need to be restarted. We only try to initialise audio once.