[SOLVED] Can't load audio file

Hello!

I’m building a tank shooter game and I’m currently trying to add sounds, however, it won’t load the sound file.
I get this error:

Microsoft.Xna.Framework.Content.ContentLoadException was unhandled
  HResult=-2146233088
  Message=Could not load Sounds/TankFire asset as a non-content file!
  Source=MonoGame.Framework
  StackTrace:
       vid Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
       vid Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
       vid TankGame.AudioManager.AddSound(String folder, String name, Single volume) i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\AudioManager.cs:rad 23
       vid TankGame.Tank.Update(List`1 tanks) i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\Tank.cs:rad 72
       vid TankGame.Screens.GameScreen.Update(Game1 game) i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\Screens\GameScreen.cs:rad 46
       vid TankGame.Game1.<Update>b__11_0(BaseScreen s) i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\Game1.cs:rad 85
       vid System.Collections.Generic.List`1.ForEach(Action`1 action)
       vid TankGame.Game1.Update(GameTime gameTime) i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\Game1.cs:rad 82
       vid Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime)
       vid Microsoft.Xna.Framework.Game.Tick()
       vid MonoGame.Framework.WinFormsGameWindow.RunLoop()
       vid MonoGame.Framework.WinFormsGamePlatform.RunLoop()
       vid Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
       vid Microsoft.Xna.Framework.Game.Run()
       vid TankGame.Program.Main() i C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\Program.cs:rad 18
       vid System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       vid System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       vid System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       vid System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233088
       Message=The content file was not found.
       Source=MonoGame.Framework
       StackTrace:
            vid Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
            vid Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
       InnerException: 
            FileName=C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\bin\Windows\Debug\Content\Sounds\TankFire.xnb
            HResult=-2147024894
            Message=Det gick inte att hitta filen C:\Users\Timmy\Dropbox\Visual Studio Projects\Skolan\TankGame\TankGame\bin\Windows\Debug\Content\Sounds\TankFire.xnb.
            Source=mscorlib
            StackTrace:
                 vid System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                 vid System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
                 vid System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
                 vid Microsoft.Xna.Framework.TitleContainer.OpenStream(String name)
                 vid Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName)
            InnerException: 

I use this code:

public void AddSound(string folder, string name, float volume)
        {
            SoundEffect sound = Content.Load<SoundEffect>(folder + "/" + name);
            Audio audio = new Audio(sound);
            audioList.Add(audio);
        }

and I do have the soundfile in Content\Sounds\name.xnb
I’ve tried .wma, .wav and .xnb files as well as changing the code to try with and without the extension. Also, the file is set to copy if newer but I’ve tried never copy as well.
Also, if you see something you don’t understand in the error it’s Swedish.

Do anyone know what I should do?

Thanks in advance!

Hi!

You have to pre-compile them using the MonoGame.ContentPipeline (make it a XNB file).
Then place it in the folder (sounds right in your description; The folder is called ‘Content’ and that’s the root-folder MonoGame starts looking for asset files; So leaving the path empty would be looking for the file directly in the ‘Content’ folder) and leave out the extension. All asset files are XNB files, so MonoGame expects you to omit it.
Forget about loading a WAV or WMA directly, also forget tampering with the copy if newer or not - thingy. Leave it to copy if newer. That’s right for most cases.
Then, and I think you are missing this: Switch the build-action on this file to ‘Content’ in the properties window.

I think you have it set to ‘Resource’ now. That way I get your exception.

Hope that helps.
Further questions, just post or write a pm.

cu,
Psilo

Hello @throbax!

Thanks for the answer! It still won’t work. I changed to xnb instead and deleted the other file formats in the folder in order to eliminate conflicts. I also have the properties set to Content, Copy if newer. Now it’s telling me the file doesn’t exist instead even though it does. I tried to send a pm but I can’t find a button to do so. If you want ask questions etc you can send a pm. If it would be easier I can zip the project and send it to you or upload it to github.

Edit: I excluded the xnb files and included them again. The properties is Content, Copy if newer. I have the same problem as before again where it can’t load instead of not being able to find the file

Thanks in advance!

Although I’m pretty sure that you’re just loading the file WITH the extension, go ahead and send the code.
Maybe I find the time in an hour or so to take a look at it.

Sorry for the thing with the pm. I remember searching for that myself with no luck… But seems I forgot.

edit
And now I found it!
It’s on the profile-page (just click on the user’s name you want to pm) and look in the upper right corner for the ‘message’ button.
yippie

Thank you @throbax ! I’ve sent an email

That’s the way you use the content-pipeline.
You’ve added it as a MonoGameContentReference like so:

And that’s fine. But if you do so, you could use it to compile your assets on-the-fly when building.

So we’ll change the setup:
Add your wav files like this:

As a wav-file (the xnb-file will be generated for you) and set to Content and Do not copy.
Now you’ll have to add the assets you want to the content-builder. Double-click your content.mgcb file.

click add-existing folder and add your sound-source folder containing your wav (uncompiled).
The next image shows what the pipeline will do automatically for you:


Now save your changes in the pipeline tool and build your solution.
Now it works.
The tool will automatically compile your specified asset and copy it to the right location (debug or release or whatnot…)

Every time you add a file, you’ll have to add it to your pipeline using this tool (if you want to compile it into an XNB file).
The tool will build it, if newer and leave it otherwise.

For completeness…

Actually, now that I come to think about it, you don’t even have to import your wav file into the VS-Solution at all.
It suffices if it is in the pipeline-tool-project. But I import it anyways since it’s nice to see it in VS…

1 Like

Thanks! It’s working now :smiley:

Correct. Adding it to the VS project and setting it to Do Not Copy means it does nothing.