Content Pipeline doesn't work for SoundEffects and SpriteFonts.

I have a simple “Monogame Windows Project” template. Within the main directory of the project there is a “Content” folder, and inside it there’s a “Content.mgcb” file. Also, in the “Game1.cs” file in the constructor, the root directory of the content manager is set to “Content”.

Now, if I add an image into the Content folder, and set the “Copy to Output Directory” property to “Copy if newer” and the “Build Action” property to “Content”, then (assuming the name of the image is “image.png”), the following line of code inside “LoadContent” works properly:

Texture2D image = Content.Load<Texture2D>("image.png");

Great! Images work perfectly; no issues there.

As soon as I do the same thing with a spritefont, it throws an error. Specifically, I did the exact same process as above only with a file called “font.spritefont”. Then in the “LoadContent” method, I added the following line:

SpriteFont font = Content.Load<SpriteFont>("font.spritefont");

(Note: I know that you don’t have to add the file extension to the name of the file you’re trying to load, I’m just doing it to make it more explicitly clear that the file I’m trying to load is a valid type, and from my tests I’ve noticed no differences no matter if the extension is included or not.)

The above line of code gives me the error “Could not load “font.spritefont” as a non-content file.”

Very strange, I thought this would work. Then, upon doing further research, I found that the Monogame Content Pipeline only excepts .xnb files.

Alright, so I supposed I was just being stupid and all along all the tutorials were actually telling me I had to use a .xnb file. So, I used the Content Pipeline to try to build my spritefont into a .xnb file. I double clicked “Content.mgcb” in my Content folder, then I added “font.spritefont”, and then I pressed “Build”.

Hilarity then ensued, as the program gave me this error:

Build started 04/05/2015 8:45:26 PM
C:/Users/Salvatore/Dropbox/FCDM/Project Phosphaze/Phosphaze/Game1/Game1/Content/font.spritefont
Building Font C:\Windows\Fonts\SegoeUIMono-Regular.TTF
System.DllNotFoundException: Unable to load DLL ‘freetype6.dll’: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at SharpFont.FT.FT_Init_FreeType(IntPtr& alibrary)
at SharpFont.Library…ctor()
at Microsoft.Xna.Framework.Content.Pipeline.Graphics.SharpFontImporter.Import(FontDescription options, String fontName)
at Microsoft.Xna.Framework.Content.Pipeline.Processors.FontDescriptionProcessor.ImportFont(FontDescription options, Single& lineSpacing, Int32& yOffsetMin, ContentProcessorContext context, String fontName)
at Microsoft.Xna.Framework.Content.Pipeline.Processors.FontDescriptionProcessor.Process(FontDescription input, ContentProcessorContext context)

According to this error I’m missing some dll called “freetype6.dll”. I did some google searching and only turned up one useful page, which told me I was missing “zlib1.dll”. I couldn’t figure out how to install it, so I just decided to move on.

My next mission was to figure out how to load SoundEffects, but before we get into that, something really strange happened. The line of code calling Content.Load<SpriteFont>("font.spritefont") was now throwing a different error than before: The method or operation is not implemented.

… What? Now the method for loading SpriteFonts simply isn’t implemented? That’s crazy, what on earth happened? I didn’t change anything so far as I can tell, I did press “Clean” in the Monogame Content Pipeline but I don’t think that would have any effect on this though, would it?

Even stranger is that now when I create a completely empty “Monogame Windows Game” solution template, and try to do the same things as above, I get the same error, telling me the method or operation is not implemented. What is happening now?

Whatever, maybe if we try to figure out how to load sound effects, we’ll learn something about the Content Pipeline that will help us with the above problem. So I tried doing the same process as with the original image, only this time with a file called “sound.wav”. Then I added the following line of code to “LoadContent”:

SoundEffect sound = Content.Load<SoundEffect>("sound.wav");

This time it gave a slightly different error. It didn’t say “could not load “sound.wav” as a non-asset file.”, it just says “could not load asset “sound.wav”.” Okay, what if we changed it to “Audio” instead?

Audio sound = Content.Load<Audio>("sound.wav");

That gives the same error.

After that, I don’t know what happened, but it started telling me Content.Load<SoundEffect> was not implemented! I have no clue what I changed, probably nothing, and it just started giving me this error!

Even weirder is that I can’t find any information on the internet about other people having this problem where the ContentManager just sporadically stops working like this. I don’t even know what I could change that could cause an error like this, let alone what was actually causing it.

Now I’m completely stuck. Any ideas?

The ability for the ContentManager to load raw assets such as .png and .wav was a temporary measure until we had a more complete content build pipeline in place. Now that we have that, the loading of raw assets through the ContentManager will be removed. I would like to see this removed before the 3.5 release since it is no longer necessary.

The ContentManager is designed to load .xnb files. These are files that have been imported and processed by the content build pipeline. The content build pipeline is driven by mgcb.exe, a command-line tool. It loads files such as Content.mgcb that the project templates provide, imports and processes each file listed in the .mgcb file and outputs .xnb files. The Pipeline GUI is a visual front-end to manage the .mgcb file and optionally build the content. The current MonoGame project templates already contain the MSBuild magic to automatically build the content (based on the Content.mgcb file in the project) and copy the .xnb files to the correct place when the project is built in Visual Studio.

The process for adding content to your project is

  • Place the raw assets (.png, .wav, etc) in your Content folder. Do
    not add them to the Visual Studio project.
  • Open the Pipeline GUI by double-clicking on the Content.mgcb file in the Visual Studio project.
  • Add the assets to your content project.
  • Save the content project and close the Pipeline GUI.
  • Go back to Visual Studio.
  • In your LoadContent() method, load the content through the Content object, without the file extension. The ContentManager automatically adds the .xnb file extension when loading the file.

A .spritefont file that you create in Pipeline GUI is an XML file that defines how the SpriteFont is to be created. The .spritefont file is treated as a raw asset that is imported and processed by the content build pipeline. As to why it couldn’t load freetype6.dll, I’m not sure. That file is distributed with the MonoGame installer. Are you running on 32- or 64-bit Windows? The content build pipeline supports 64-bit Windows only as the native libraries that we supply are all 64-bit versions.

Declare at the top of your class

Texture2D _texture;
SoundEffect _sound;
SpriteFont _font;

In your LoadContent() method, add the following. I used the same filenames you used above, but notice the extensions have been removed.

_texture = Content.Load<Texture2D>("image");
_sound = Content.Load<SoundEffect>("sound");
_font = Content.Load<SpriteFont>("font");

I hope that helps. Post here if you have any further queries.

1 Like

Wow! First off, thank you for such a complete answer, that clears up a lot of confusion!

I followed all the steps and everything works perfectly, with images, sounds, and spritefonts. Thank you so much!