Table of Contents

How to play a Sound(effect)

This topic demonstrates how to play a simple sound by using SoundEffect.

Overview

In this example we will take our first steps into audio nirvana and play a simple one-time-only SoundEffect.

Requirements

This sample uses a sound file named tx0_fire1.wav, which you can download from this link here. (save-as) But you can use your own if you wish.

To add a sound file to your project

  1. Open your MonoGame game in your editor of choice.

  2. Open the Content.mgcb file by either double-clicking on it, or right-clicking and selecting `Open in MGCB editor". (depending on your editor)

  3. Click Add in the menu bar, and then click Existing Item. (alternatively, use the icon for the same task in the editor)

  4. Navigate to the sound file you want to add, and then select it.

The selected audio file is inserted into your project. By default, it is processed by the Content Pipeline, and built wave files are accessed automatically when the game is built.

To play a simple sound

  1. Declare a SoundEffect object to hold the sound file.

    // Audio objects
    SoundEffect soundEffect;
    
  2. Load the sound file using Content.Load in the 'LoadContent' method.

    soundEffect = Content.Load<SoundEffect>("tx0_fire1");
    
  3. Play the sound.

    // Play the sound
    soundEffect.Play();
    
Note

Obviously you would not normally call play in the LoadContent method, we just use here as an example.

See Also

Reference

© 2012 Microsoft Corporation. All rights reserved.