Removing a SoundEffectInstance without using 'dispose' in C# XNA - c#

I've run into a slight problem. When I restart my game (by running my own initialize method), I can successfully reload all my objects/variables. But the initial SoundEffectInstance I used continues to play while a new instance is created. This results in multiple instances of the same music being played simultaneously.
What I'm trying to achieve is a way to get rid of the first instance when making a new one.
instance.Dispose(); can't be used as it means I can't re-initialize the sound.
I tried using a list to solve this (removing instances before restarting game), but it seems I can't CreateInstance() from within the list, restricting me from initializing the sound to begin with.
Arrays don't seem to offer a solution either. Any ideas?

First off, you probably shouldn't use SoundEffectInstance for music - this results in the entire song being loaded into RAM uncompressed - which eats up a ton of space relative to streaming an MP3/WMA from disk using Music.
If you want to dispose all content then you should unload the ContentManager that has all of your content. Then you can recreate the content - it will take some time to re-load it from disk but you will have a clean set of content if that's what you're after. Keep in mind that a SoundEffectInstance contains unmanaged resources, so the only way to "remove" it is to dispose it or unload its ContentManager.
If you don't want to do that then you should be preserving references to your content and then you can manually do things like stop:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.stop.aspx

You should use mediaplayer.play() to play a song instead of SoundEffectInstance and then
you can just have a if statement that you can say if X happens then mediaplayer.play()
else
mediaplayer.stop(); or
mediaplayer.pause()

Related

What is the fastest way to load Textures in XNA

I'm a student using the latest version of Microsoft XNA for learning purposes.
I need to load a whole lot of texture from the Content folder (or more specifically a folder called Tiles inside the Content folder) and I really don't want to type the following code 100 times:
texture = Content.Load<Texture2D>("texture");
I realize I could change the names of the files and run through a loop to load them, but this seems inefficient for finding specific tiles if there's a way I could keep the names. I am just looking for a way to load all of these that saves development time.
Also, the files are in .BMP form, if that matters.
Keep in mind that every texture needs to be loaded at least once.
So using
texture = Content.Load<Texture2D>("texture");
is fine to repeat for different textures, as long as you don't repeat it for the same texture.
I've learned is to load all textures in the LoadContent() of game1.cs, that method is only called once at the moment the game starts. So they're all ready to be used for the rest of the game.
There's also a Lazy Loading practice, where the texture is only loaded for when the object appears. But you should be aware that the texture loading won't repeat itself for each object.

Monogame SoundEffect Crash

I am trying to re-program some old Arcade games in C# with the Monogame engine. However, I have encountered a certain problem.
Since my code tends to be a bit messed up, I am often not taking the effort to reset everything when the player successfully completes the game. Instead, I am simply closing the current Game-instance and opening a new one, like this: (in Program.cs)
if (startgame)
{
do
{
using (var game = new Game1(level, points, soundOn))
game.Run();
} while (continueGame == true);
}
Now the problem. In Game1, I am declaring and playing various SoundEffects. The first run everything works fine, but in all following Game1-instances, my program will always throw an System.AccessViolationException related to SharpDX.XAudio2.dll at the moment I am calling the .Play()-Method of a SoundEffect.
I tried playing SoundEffectInstances instead of the actual SoundEffects. Now it doesn't crash anymore, but is being completely silent from level 2 on instead.
Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Thank you in advance.
(I am using Win 7 64 Bit, VS Express 2015 and Monogame 3.6)
Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Yes. Do not do it.
You are destroying the whole game object just to reset the game. This is a very bad style, because it destroys the whole instance and everything that is connected to it. If you use static variables within it, they will remain in memory which may be the problem you have.
If you want to reset your game, simply write a Reset-method for everything and call these instead. Cleanup and nice code is something you should use instead of terrible hacks.
Can you show more code, so it is possible to understand, what went wrong?
PS: Never write something like while (continueGame == true) always go for while (continueGame).
There is a chance that the issue is due to SharpDX's reaction to the media file itself. For instance, an MP3 file that would work perfectly fine under XNA 4.0 loaded as a SoundEffect (with associated SoundEffectInstance) may crash the game under MonoGame UWP. (This happened to me with one file, but I was already using SoundEffectInstance in the first place when the crashing started.)
As such, have you tried re-encoding your relevant audio files, such as through a utility like Audacity? Although it may not solve the internal issue per se (some element of the audio file breaking SharpDX), if a re-encoded file doesn't have the problem element in the first place, that should be a valid solution for you.

Preloading audio file with winmm.dll in C#

I am writing a wave audio player using C# and winmm.dll. To reduce the delay while switching from one file to the next, I open the next file with a different alias. While switching to the next file, I stop the first alias, and then play the second one.
Everything works great, until the user waits a while (i.e. 20 minutes) before playing the next (preloaded) file. In this situation the player has a 5 seconds delay before playing the new file, which has been preloaded for a while.
The question is, does winmm.dll put not-used-files in a "standby" mode? Do we need to activate it in certain time periods to reduce the delay? If yes how to do that?
I found a status called "parked" but this is never returned, and I can't find anything on the web describing it.
It would be a great help, if someone could help, cause there is not much documentation out there for this DLL.
MCI Commands
Thanks in advance!

Monogame content use efficiency

I've been using Monogame for a awhile now and I was just wondering what is the best way to load my content? Lets say i have an intervals system that constantly creates objects on the screen, so should I load the object's sprite in the game class and put the sprite variable in the constructor or should I put the content variable in the object constructor and load the sprite from within the object?
Btw by best way I mean I try to keep the framerate and use less memory, thx in advance!
Best way for frame rate is to load it before game loop starts. But the best way for less memory is right when you need it and dispose of it after your done. Not necessarily mutually inclusive... pick your poison.
You have 3 different solutions, and each will fit a different type of game.
Load everything in LoadContent
This will cause a longer initial startup time, but after everything is loaded, you wont have to wait for anything anymore. However keep in mind that this is only suited for small games such as Tetris, Arcanoid or Chess. Games that generally doesn't have a lot of content. Keep in mind that the more content you load, the more memory it will consume.
Load everything needed for the current scene
This is what most games does, since you wont load data for scenes that you never use, and you wont load data for scenes you're not yet accessing.
Load everything needed for the current scene (Extended)
Like the previous answer, but with a small twist. If possible, load content while displaying other content. For instance, as soon as the player finished the last step for completing a scene, initiate the loading of the next scene.
Arcanoid example: As soon as the ball hits the last block (or if you're brave, even as soon as you can calculate that the ball WILL hit the final block), initiate the loading of the content for the next scene. Let this load while the ball flies towards the final block, and also while displaying the score for the current scene (Time, deaths, bonuses, etc.)
And should the player actually close the dialogues before the scene has been loaded, show another loading scene while the data finishes loading. That way, there might only be 1-2 seconds of loading time instead of 10.
Remember to perform all this loading in a background thread.

ContentManager Out of Memory before loading any data

I'm trying to load data in XNA, however, whenever I use the ContentManager it throws an out of memory exception. This occurs when I load my first resource. It's a 32x32 pixel image.
I'm using:
contentManager = (Application.Current as App).Content;
contentManager.Load<Texture2D>("Head");
I've been using this for ages and have no clue why it won't work now. Does anyone have any suggestions to get me past this?
I've experienced this one time, it would occur consistently after coming back from a phone task (in my case it was the IAP interface).
I worked around it by delaying the content load. My assumption was that the IAP task itself was using a lot of memory and I needed to give some time for those resources to free up before trying to allocate any more resources.
You could try adding a delay before doing the content loading (not sure when you are doing it, it should be fine if you are doing it in a place like Game.Initialize though).

Categories