5
\$\begingroup\$

I've been using verbatim string literals for some time now, and never quite thought about using a regular string literal until I started to come across them in Microsoft provided XNA samples. With that said, I'm trying to implement a new AudioManager class from the Net Rumble sample.

I have two (2) issues here:

Question 1: In my code for my GameplayScreen screen I have a folder location written as the following, and it works fine:

menuButton = content.Load<SoundEffect>(@"sfx/menuButton");
menuClose = content.Load<SoundEffect>(@"sfx/menuClose");

If you notice, you'll see that I'm using a verbatim string, with a forward slash "/".

In the AudioManager class, they use a regular string literal, but with two backslashes "\". I understand that one is used as an escape, but why are they BACK instead of FORWARD? (See below)

soundList[soundName] = game.Content.Load<SoundEffect>("audio\\wav\\"+ soundName);

Question 2: I seem to be doing everything correctly in my AudioManager class, but I'm not sure of what this line means:

audioFileList = audioFolder.GetFiles("*.xnb");

I suppose that the *xnb means look for everything BUT files that end in *xnb? I can't figure out what I'm doing wrong with my file locations, as the sound effects are not playing. My code is not much different from what I've linked to above.

private AudioManager(Game game, DirectoryInfo audioDirectory) : base(game)
{
    try
    {
        audioFolder = audioDirectory;
        audioFileList = audioFolder.GetFiles("*.mp3");
        soundList = new Dictionary<string, SoundEffect>();

        for (int i = 0; i < audioFileList.Length; i++)
        {
            string soundName = Path.GetFileNameWithoutExtension(audioFileList[i].Name);
            soundList[soundName] = game.Content.Load<SoundEffect>(@"sfx\" + soundName);
            soundList[soundName].Name = soundName;
        }

        // Plays this track while the GameplayScreen is active
        soundtrack = game.Content.Load<Song>("boomer");
    }
    catch (NoAudioHardwareException)
    {
        // silently fall back to silence
    }
}
\$\endgroup\$

2 Answers 2

7
\$\begingroup\$
  1. Different file systems use different delimiters between folders in paths. Unix/Linux based OSes use forward slashes. Windows generally uses backslashes however they also accept forward slashes. I personally use forward slashes but that's just out of habit since I spend some of my time on OS X.

  2. GetFiles("*.xnb") will be looking only for XNB files in that directory (see http://msdn.microsoft.com/en-us/library/8he88b63.aspx). Have you stepped through in the debugger to see what files it's finding, if any? When you create the AudioManager, how are you getting/creating that DirectoryInfo object? Is it pointing at the correct directory?

\$\endgroup\$
5
  • \$\begingroup\$ Thanks Nick. I understand #1 now. I'm not quite sure of how to debug for this. I have my AudioManager.PlaySoundEffect("menuButton"); method in the same location as the Net Rumble sample, so I don't think that is the issue. I'm fairly certain that the file location is correct, as I copy + pasted it from my GameplayScreen, where I originally had the audio working on a sound effect-by-effect basis before creating the audio manager. My files end in .Mp3(NOT made using Xact), whereas the sample uses .wav and I believe is done in Xact, hence he .xnb \$\endgroup\$ Commented May 30, 2012 at 2:02
  • 1
    \$\begingroup\$ Since I can't comment on Joel's answer, the reason to search for XNB files is that XNA Game Studio will take your WAV files (for sound effects) and compile them into XNB files compatible with ContentManager.Load<SoundEffect>. If you have WAV files in your output, because you skipped the content pipeline or otherwise, you'd want to use the SoundEffect.FromFile method instead. \$\endgroup\$ Commented May 30, 2012 at 2:06
  • \$\begingroup\$ Is that the same case for mp3? \$\endgroup\$ Commented May 30, 2012 at 2:07
  • 3
    \$\begingroup\$ Nice answer Nick. Welcome to GDSE, I'm sure you'll do fine here. \$\endgroup\$
    – House
    Commented May 30, 2012 at 2:16
  • 1
    \$\begingroup\$ MP3 files have to be compiled by the content pipeline into XNB files to be used with the SoundEffect API. SoundEffect.FromStream (sorry for the wrong method name in the last comment) only accepts WAV data. If you're using MP3 files you either must use Song.FromStream and play them with the MediaPlayer class or you must compile them with the content pipeline which will produce an XNB you can load with the content manager. \$\endgroup\$ Commented May 30, 2012 at 2:16
1
\$\begingroup\$

For question #1, the answer is that it doesn't really matter ... the path will work whether you use forward or backwards slashes. More specifically, the double backslash is because in a regular string the backslash is an escape character ... so if you want an actual backslash you have to use two of them (to escape the escape character). But if you use the verbatim string, you could do:

soundList[soundName] = game.Content.Load<SoundEffect>(@"audio\wav\"+ soundName);

the * character is a wild card ... so it means to list only files with the xnb extension

\$\endgroup\$
1
  • \$\begingroup\$ 1) Understood. 2) Understood as well. Hmmmm I wonder why they use *xnb then, when all of their extensions end in .wav (mine are .mp3). Perhaps because it is taken from Xact. Thanks Joel! \$\endgroup\$ Commented May 30, 2012 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .