Best way to approach sound with XNA?

Started by
1 comment, last by walnut100 15 years, 4 months ago
Hey guys, I've dabbled on and off for a long time with DirectX and XNA, never anything too serious, but I decided recently to try to write a little "engine" for myself to load and play sounds in XNA3, without actually writing a game yet. What I wrote works beautifully, but is not memory efficient at all, as it loads up all the songs at the start of the program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace XNAtree
{
    public class Sound
    {
        public static Dictionary<string, Song> SongLibrary = new Dictionary<string, Song>();

        public static void LoadSongs(ContentManager content)
        {
            List<string> SongList = new List<string>();

            SongList.Add("Track 1");
            SongList.Add("Track 2");

            foreach (string key in SongList)
            {
                SongLibrary.Add(key, content.Load<Song>(key));
            }
        }

        public static void PlayMusic(string song)
        {
            if (SongLibrary.ContainsKey(song))
            {
                MediaPlayer.Play(SongLibrary[song]);
                MediaPlayer.IsRepeating = false;
            }
        }

        public static void LoopMusic(string song)
        {
                if (SongLibrary.ContainsKey(song))
                {
                    MediaPlayer.Play(SongLibrary[song]);
                    MediaPlayer.IsRepeating = true;
                }
        }

        public static void StopMusic()
        {
                MediaPlayer.Stop();
        }
    }
What is the best way to go about using memory efficiently with sound? Are there any good tutorials around that might help me with this? Thank you very much.
Advertisement
So far I just load up all my sounds, and when it comes time to play them, I just make the call Fire and Forget style. My content is fixed, so I don't have to worry about dynamically loading anything.

Also, there is a 16 sound effect at a time limit in the mixer, but I don't need to worry about that either, because if I had that many sound effect instances playing, it would be made up of similar sounds that would just blend together anyways, and no one would hear the difference.

That would work well for me, except I'm using a static dictionary at the moment to organize sounds, which loads everything up front.

Perhaps if I apply my system to a more OOP style, where I treat each room of the game as an object, I will yield better results? Or am I totally off base on this?

EDIT:

Nevermind, problem solved, at least for a while. This article helped me immensely. I suggest that other newbies with the same issue read it.

http://www.ziggyware.com/readarticle.php?article_id=204

[Edited by - walnut100 on December 2, 2008 4:24:26 PM]

This topic is closed to new replies.

Advertisement