Casting an Audio resource


  • My project contains a number of Audio resources and the following snippet works correctly:

                SoundPlayer s = new SoundPlayer(Resource1.el);

    However I want to store these resources in a Hashtable as follows:

                Hashtable opt = new Hashtable();

                   opt["el"] = Resource1.el;

    Now I want to retrieve the resource and play the sound as follows:

                ??? res = (???)opt["el"];
                 s = new SoundPlayer(res);


    As you can guess, I don't know how to cast the Hashtable item correctly. Please hetlp!

    Saturday, April 8, 2017 6:51 PM

All replies

  • Perhaps a little better stated, I would like to extract an object stored in my Hashtable and cast it as follows:

               Object obj = opt["el"];

                ??? res = (???)obj;

                s = new SoundPlayer(res);


    So the question really is, what type of object is an Audio Resource?

    I would really appreciate your help.

    Sunday, April 9, 2017 2:06 PM
  • Hi friend,

    I will move it to C# forum for you to get a professional and quick answer.

    Thanks for your understanding.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, April 10, 2017 2:25 AM
  • MCorazoaoSR,

                 If you go by help, you can see the SoundPlayer constructor either accepts Stream or .wav file.

    Stream is an audio stream only.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, April 10, 2017 3:35 AM
  • I found that "Resources" are stored as copies of input files. So, a sound resource is also stored as a file (in a location that the compiler knows). In the example I posted, the Hashtable opt would contain a collection of file names. The code I was trying to write should look like this:

       Hashtable opt = new Hashtable();

                   opt["el"] = Resource1.el;

                string res = (string)opt["el"];
                 s = new SoundPlayer(res);


    Wednesday, May 10, 2017 4:56 PM