audioxna-4.0soundeffect

XNA SoundEffects not playing


Followed a few tutorials and I'm having no luck, I'm trying to add a simple .WAV sound effect into XNA using free samples, I have named the sound as:

SoundEffect hit1;
SoundEffect hit2;

And then loaded the content with:

hit1= content.load<SoundEffect>("hit1")

But when it comes to adding the play to a button press and I go to test it there's no sound at all even no errors or nothing the game loads and is playable but any sound effects are not working.

** // Sounds

        SoundEffect hit1;
        SoundEffect hit2;

This is my variable names:

// Sounds

            hit1 = Content.Load<SoundEffect>("hit1");
            hit2 = Content.Load<SoundEffect>("hit2");

This is how I'm loading them in the loadcontent method:

//If Keyboard Key W is Pressed or Buttong Y
                if (keys1.IsKeyDown(Keys.W) && oldKeys1.IsKeyUp(Keys.W)
                    || (oldpad1.Buttons.Y == ButtonState.Released) && (pad1.Buttons.Y == ButtonState.Pressed))
                {
                    secondsPassed = 0;
                    // IF The Target Image is a Gnome 
                    if (targets[0] == sprites[0])
                    {
                        //They whacked a gnome
                        GnomeHits = GnomeHits + 1;
                        runNum = 0;
                        secondsPassed = 0;
                        hit1.Play();

                    }
                    else
                    {
                        //They whacked a troll
                        scoreNum = scoreNum + 1;
                        runNum = runNum + 1;
                        GnomeHits = 0;
                        secondsPassed = 0;
                        hit2.Play();
                    }
                    SetUpSpriteLoop();

And this is one of the control buttons I'm trying to assign sound too when I hit F5 and run the game when I hit the key or button no sound at all.


Solution

  • My first recommendation would be to make sure you are updating the keyboard presses. I only say that as I can't see you doing that in your code. If you do not do this then the key states will not update and therefore you will never enter the if statement.

    Once you have ensured you are entering the if statement, I would play around with the volume of the sound effect to make sure it is actually audible. I may be a bit off base there however if I am I suggest following this tutorial rbwhitaker is a great resource for XNA.