Hey guys, I have a million questions but the main one I need ahelp with is that I have a method in a class that plays sound and I am trying to call that method in the mainscreen class. So I create a new instance of the class that contains the makenoise method but when I create a new instance and use the method it does not work.

Also if I leave the programme to run for a bit it tells me an out of memory error, I spent hours last night trying to solve these errors but I'm at a dead end, can anyone here make any suggestions please?

Here is some code that I am using I dont want to paste it all just the parts I have written that are supposed to function:

class Alien: Enemy
    {
        public SoundPlayer aliensound = new SoundPlayer(Properties.Resources.scream2);


  public override void MakeNoise()
        {
            aliensound.Play();
            
        }



    public partial class MainScreen : Form
    {
        Alien alien;
       
alien = new Alien(pictureBox1, GamePanel);


   alien.MakeNoise();//this code is used in the paintevent method after an alien is removed this sound should play for it dyiing

Many thanks for any input whatsoever!

Dani AI

Generated

Likely causes: the sound object or its stream is short‑lived (or you’re creating many players), and playback is happening asynchronously so the player/stream can be gone before the sound finishes. The out‑of‑memory symptom points to repeatedly constructing SoundPlayer/streams (for every alien or inside Paint), which will chew memory and native handles over time.

Why this happens: SoundPlayer.Play() is non‑blocking. If you construct the player (or pass a Stream) inside a one‑off method, the CLR can collect the object or the stream may be closed before playback completes. Also creating one SoundPlayer per entity inside a tight loop or Paint event quickly accumulates resources. was right to point at scope; ’s format/size tip is relevant too. See the official SoundPlayer notes for details: SoundPlayer docs.

Practical fixes (apply one or more):

  • Centralize sound objects. Create and pre‑load a single SoundPlayer for that effect and reuse it for every alien death. Example pattern:
public static class AudioManager
{
    private static readonly SoundPlayer _scream;

    static AudioManager()
    {
        _scream = new SoundPlayer();
        _scream.Stream = Properties.Resources.YourSoundResource;
        _scream.Load(); // synchronously load into memory once
    }

    public static void PlayScream() => _scream.Play();
}
  • Call AudioManager.PlayScream() from the game logic that removes the alien — not from the Paint event. Paint should only render.

  • If you must create a transient player, use PlaySync() so the call blocks until playback finishes (not ideal for UI threads).

Troubleshooting checklist:

  • Put a Debug.WriteLine when MakeNoise runs to confirm the call path and frequency.
  • Temporarily replace your resource call with System.Media.SystemSounds.Beep.Play() to confirm system audio works.
  • Monitor memory in Task Manager while reproducing the problem; if memory climbs, look for repeated allocations (players/streams/bitmaps).
  • If using a Stream to initialize SoundPlayer, keep that stream alive for playback or pre‑load as shown.

These steps should stop the intermittent silence and prevent the memory leak.

Recommended Answers

All 3 Replies

Hello, thanks for your reply. The file itself works as I can crete the object and link to the reource file and play it in a class by calling the object.Play(); within the same class, but I have a makeNoise method containg the object.Play(); which I want to call in another class by creating an instance of the class containing the makeNoise method and then typing instancename.makeNoise();

It doesnt show any errors but it does not play the sound, I do not know if its possible to do this in C#, I am new to this and trying to figure things out.

Since I tried this I keep getting out of memory errors, I have no clue how to fix this, I've been on it for hours.

Thanks for your help.

If you are creating the object in the method, then it is likely that it is being disposed when the method terminates, possibly before the sound has a chance to play.
Try creating the object at a higher scope level and call the makeNoise method when you need to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.