SoundEffects not working in 3.6 Version

I have tried most recommendations made here and on other sites.
Have updated DirectX to 9.0c … although using Windows 10 and Windows 7 shouldn’t that already be included. I think both systems have dx11

The songs work fine , I don’t get any errors declaring Soundeffect , loading Soundeffects or coding the play commands but nothing is heard.

Hello Mr. Valentine

Not sure what your response means? Trying to get SoundEffects to work in Monogames using Visual Studio 2015 on either a windows 10 or 7 machine

Just pointing out what DirectX is, short reply because I have grown tired over the past 7 years having to explain it :stuck_out_tongue:

Did the same code work in 3.5? Can you post the code + files you try to play?

I have posted the entire code with sound files here: http://ge.tt/49fEdYn2

I am using VS 2015 Mono 3.6 Windows 10
This is my first time using Mono so never had 3.5 installed. Have some experience using XNA 3 and 4.
Just tried Mono 3.5 on my Windows 7 machine using VS 2015
and the Mono 3.6 on Windows 7 using VS 2017 … still no luck

Here’s the code

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media; //need to add for Song
using Microsoft.Xna.Framework.Audio; //need to add for SoundEffects

namespace MonoIntro2
{
///


/// This is the main type for your game.
///

public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D spriteTexture;

    //add a sprite location variable to be used for moving the image
    Vector2 spriteLocation = new Vector2(0, 0);

    //add a velocity variable which will be used to move the image
    Vector2 spriteVelocity = new Vector2(5, 1);

    SpriteFont font;

    int count = 0;

    Song song; //plays throughout game does not support multiple instances playing at once

   
    SoundEffect soundEffect;        


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here
               

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        
        // TODO: use this.Content to load your game content here
        spriteTexture = Content.Load<Texture2D>("daffy");
        font = Content.Load<SpriteFont>("score");

        this.song = Content.Load<Song>("intro");    //mp3 work best with Song class
       
        MediaPlayer.Play(song);
        //  Uncomment the following line will also loop the song
        //  MediaPlayer.IsRepeating = true;

        MediaPlayer.MediaStateChanged += MediaPlayer_MediaStateChanged;

        //Sound Effects 
        soundEffect = Content.Load<SoundEffect>("explosion");           
    }

    void MediaPlayer_MediaStateChanged(object sender, System.
                                       EventArgs e)
    {
        // 0.0f is silent, 1.0f is full volume
        MediaPlayer.Volume -= 0.1f;
        MediaPlayer.Play(song);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// game-specific content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        // TODO: Add your update logic here
        //spriteLocation.X = spriteLocation.X + 5;
        //spriteLocation.Y = spriteLocation.Y + 1;

        //update logic above replaced by velocity
        //we are using the Vector addition concepts here ie (x1,y1) + (x2,y2) = (x1+x2,y1+y2)
        spriteLocation = spriteLocation + spriteVelocity;

        //Checking for wall collisions and then reverse paths
        if (spriteLocation.X + spriteTexture.Width > graphics.GraphicsDevice.Viewport.Width)    //right wall
        {
            spriteVelocity.X = -spriteVelocity.X;
            count++;
            soundEffect.Play();
        }
           
        if (spriteLocation.X <0)                                                                //left
            spriteVelocity.X = -spriteVelocity.X;
        if (spriteLocation.Y + spriteTexture.Height > graphics.GraphicsDevice.Viewport.Height)  //bottom
            spriteVelocity.Y = -spriteVelocity.Y;
        if (spriteLocation.Y <0)                                                                //top 
            spriteVelocity.Y = -spriteVelocity.Y;

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.DrawString(font, "Welcome to My First XNA Program " + count.ToString(), new Vector2(400, 20), Color.White);
        spriteBatch.Draw(spriteTexture, spriteLocation, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

You mean MonoGame, right?

yes , right now I have version 3.6 installed

1 Like
  1. Are you absolutely sure the line with soundEffect.Play(); is called? did you see it go there with debugger?
  2. If so, try replacing your sound file with another file.

Hi GeonBit

Yes the Play command is executed . Tried different wav file still no sound. Song files play fine.

Don’t you need to create a soundeffect instance then run soundeffect.instance.Play() ?

Does it return false? Did you try providing a volume param with 1?

Also just to make sure - what importer does it use in the content manager (I don’t think it’s the problem since it should raise exception if you try to play a song as a sound effect, but just to be sure)

No, calling SoundEffect play directly just play the sound once and manage it’s lifetime. You need instance only if you want repeating or 3d sound etc.

I was wondering if anyone could download the program I posted on ge.tt and try it out. Its a simple program that has a cartoon character moving across the screen. I have some background music playing (a song). When the object hits the right wall I have a soundeffect that is supposedly playing but doesnt.

It would be interesting if it works on anyone else’s machine.

If it does maybe I have a conflict with some other software I have running on my machine