Null reference exception when calling Play on a SoundEffect or SoundEffectInstance

I’ve just upgraded from Monogame 3.4 to 3.5 and when I call SoundEffect.Play or SoundEffectInstance.Play it crashes with a NullReferenceException, any ideas?

Here’s a sample project demonstrating my problem. Press Z or X to play a sound.

Project is DesktopGL sound is a wav file. I’m using SoundEffect.FromStream but Content.Load has the same problem.

I am sorry,
it worked for me.

I tried it with Content.Load(“SwordHit1”).

Works fine.

I mean Content.Load(“SwordHit1”);

Lol, the editor removes the arrows :slightly_smiling:

Content.Load(arrow_left)SoundEffect(arrow_right)(“SwordHit1”);
like in generic classes.

You can use the markup to get it right:
Content.Load<SoundEffect>("SwordHit1");

So I loaded the monogame source and the bug is on SoundEffectInstance.OpenAL.cs line 122, _effect.SoundBuffer is null. I’ll keep looking into it though it’s weird that it works for you.

I had a false Ahaa moment because I found this note in the code:

// Notes from the docs:

            /*The Stream object must point to the head of a valid PCM wave file. Also, this wave file must be in the RIFF bitstream format.
              The audio format has the following restrictions:
              Must be a PCM wave file
              Can only be mono or stereo
              Must be 8 or 16 bit
              Sample rate must be between 8,000 Hz and 48,000 Hz*/

And my sound is 32 bit float at 44100Hz so I changed it to 16 bit PCM at 44100Hz. This however did not actually fix anything and I still get the bug.

The game crashes because the SoundEffect.SoundBuffer (in SoundEffect.OpenAL.cs) is not set
Theres only one assignment anywhere and it’s in SoundEffect.PlatformInitialize but this is only ever called from one of the two SoundEffect constructors. The other constructor is empty and thats the one that gets called when you load the sound effect.

Without having any real understanding of the code path I copied the lines:

    SoundBuffer = new OALSoundBuffer();
    SoundBuffer.BindDataBuffer(_data, Format, Size, (int)Rate);

into SoundEffect.PlatformLoadAudioStream (in SoundEffect.OpenAL.cs) and it seems to have solved the issue. It would be great if someone who has some knowledge of the system could take a look at this and determine the optimal way of fixing this for the official branch (if it hasn’t already been fixed).

There’s no possible way it could have worked given the code. Are you using DesktopGL? And version 3.5.0?

I am sorry, I had no time to respond.

Mate,
I just add the Soundeffect-File to the content-manager, and load it with Content.Load<SoundEffect>("SwordHit1");

I am confused that it does work for me but not for you.

Here is the executeable : http://www.file-upload.net/download-11442813/Debug.zip.html

Press X, it works.(Z does not, eventually the volume is set zero)

EDIT: Did you install OpenAL?
(C:\Program Files (x86)\MonoGame\v3.0\oalinst.exe)

@poohshoes - If you think you’ve found a critical bug please post an issue to GitHub:

https://github.com/mono/MonoGame/issues

We would want to fix this and make sure it is in the next patch release.

Ahh So I am trying to load raw wav files, I assume that adding it to the content project compiles it into a xnb file.

If you have added it into the content-manager, of course.

See the zip-File I have uploaded.
Scroll to Content-Folder.

It will contain the .xnb file.

@poohshoes, the fix for PlatformLoadAudioStream in SoundEffect.OpenAL.cs was merged 18 days ago in the develop branch. It doesn’t appear to have made it into the 3.5.1 patch release.

1 Like