views:

333

answers:

1

I have a Compact Framework 3.5 application that is responsible for scanning barcodes. There are 3 different sounds it should play depending on the situation, so I created a wrapper class around the SoundPlayer object and call the Play method on that.

public static class SoundEffectsPlayer
{
    private static readonly SoundEffect _alert;

    static SoundEffectsPlayer()
    {
        // This will cause the SoundEffect class to throw an error that the Uri 
        // Format is not supported, when new SoundPlayer(location) is called.
        _alert = new SoundEffect("SoundEffects/alert.wav");
    }

    public static SoundEffect Alert
    {
        get { return _alert; }
    }
}

public class SoundEffect
{
    private readonly SoundPlayer _sound;

    public SoundEffect(string location)
    {
        _sound = new SoundPlayer(location);
        _sound.Load();
    }

    public bool IsLoaded
    {
        get { return _sound.IsLoadCompleted; }
    }

    public void Play()
    {
        _sound.Play();
    }
}

The idea was to not create a SoundPlayer each time a barcode needed to be scanned (which would be a few hundred times an hour). So I can just call Play on the already loaded file.

The alert.wav file is in a SoundEffects folder that is on the root of the library project that the application references, and it is set to be an Embedded Resource. What would I need to pass in to the SoundEffects class to load the wav file? Should the wav file even be embedded into the library project?

Also does anyone notice anything wrong with the way I'm handling the playing of the wav files? This is my first attempt at something like this, so I'm open to suggestions for improvement.

+2  A: 

Hmmm. I haven't used SoundPlayer before, i'm guessing it is just a wrapper around the coredll function PlaySound. But anyhow, I would expect that it demands a file path as the argument.

Therefore to make it work with an embedded resource you would probably have to save the file to disk and then play it. It would probably be simpler to scrap the embedded resource idea and deploy it as a separate item. Include the .wav in your main project set it to: "Content" and "Copy if newer" and then reference it as a file in the sound player call.

Remember that in WindowsCE you always need FULL paths as well. A relative path from the currently running .exe wont work in CE.

If you need to find out "where you are" to create the path to the resource, see the answer to this question.

Quibblesome
Thanks, it works great now.
Brandon