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?
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).
@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.