0

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?

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
  • 1
    How about stopping and discarding the soundeffectinstance completely, by reinstantiate it when the game restarts? That way, after X amount of time, the garbage collector will come and pick it up, and it wont be a problem anymore. – Falgantil Nov 19 '14 at 09:34
  • @Bjarke: By discarding it completely, do you mean to `dispose()` of it? Or do you mean using the `unloadcontent()` method? –  Nov 19 '14 at 13:51

2 Answers2

1

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

Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49
0

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()

koss
  • 3
  • 3