1

I have a folder with a number of .wav files inside. I need to dynamically count these files in order to generate the sound in a random way. I need this dynamically, so I don't need to update code everytime I update the sound folder with another sound. I searched how to do it, but could only found examples for Windows. Here's the code I came up with:

string path = string.Format("/Sound/{0}", sourceSound);
return Directory.GetFiles(path, ".wav").Length;

I tried running it, but VS gives me the error:

"Unable to step. The code is currently unavailable."

Is there anything I'm doing wrong, or any other case how can we count the number of files inside a folder?

Thanks.

2 Answers2

0

Try

using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            var i = myIsolatedStorage.GetFileNames("*.wav").Length;
        }
Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
0

For some reason I couldn't make the example Vitalii gave me work. I was never able to get the count of files I have in the volder. Looking over the internet, I stumbled upon this link:

confused about resources and GetManifestResourceNames()

The answer Zack gave on this thread, gave the insight I needed to make my App work.

I used Embedded Resource to find all the count of files I needed and, then, play the with a SoundEffectInstance. The following link helped on this:

http://matthiasshapiro.com/2011/01/10/embedding-a-sound-file-in-windows-phone-7-app-silverlight/

Here's how I managed to make it work:

soundCount = GetSoundCount();

private int GetSoundCount()
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceNames().Where(name => name.Contains(sourceImg)).Count();
        }

With these few lines I managed to get the exact number of files I have in my App.

To make the sound play, I used the second link as an example and produced the code below:

if(soundInstance != null)
soundInstance.Stop();
this.soundInstance = SetNextSource();
soundInstance.Play();


private SoundEffectInstance SetNextSource()
        {
            Random random = new Random();
            Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            Stream stream = assembly.GetManifestResourceStream(string.Format("Assembly Name.Folder.{0}.{1}.wav", SubFolder, FileName + random.Next(soundCount)));
            SoundEffect se = SoundEffect.FromStream(stream);
            return se.CreateInstance();
        }

Well after a few days o research finally managed to make it work. Hope this thread helps people facing the same problem I did.

Thanks.

Community
  • 1
  • 1