Click here to Skip to main content
15,899,474 members
Home / Discussions / C#
   

C#

 
QuestionPlay WAV file from embedded resources. Pin
ranzask27-Jan-06 15:34
ranzask27-Jan-06 15:34 
AnswerRe: Play WAV file from embedded resources. Pin
[Marc]27-Jan-06 16:30
[Marc]27-Jan-06 16:30 
GeneralRe: Play WAV file from embedded resources. Pin
ranzask27-Jan-06 23:49
ranzask27-Jan-06 23:49 
GeneralRe: Play WAV file from embedded resources. Pin
Curtis Schlak.28-Jan-06 7:40
Curtis Schlak.28-Jan-06 7:40 
AnswerRe: Play WAV file from embedded resources. Pin
Sean Michael Murphy27-Jan-06 17:25
Sean Michael Murphy27-Jan-06 17:25 
GeneralRe: Play WAV file from embedded resources. Pin
ranzask27-Jan-06 23:48
ranzask27-Jan-06 23:48 
GeneralRe: Play WAV file from embedded resources. Pin
Saravanan_article29-Jan-06 22:02
Saravanan_article29-Jan-06 22:02 
GeneralRe: Play WAV file from embedded resources. Pin
ranzask30-Jan-06 0:05
ranzask30-Jan-06 0:05 
the final code is:

<br />
using System;<br />
using System.Runtime.InteropServices;<br />
using System.Diagnostics;<br />
using System.Threading;<br />
using System.IO;<br />
using System.Reflection;<br />
namespace yournamespace<br />
{<br />
    public class WSounds<br />
    {<br />
        private byte[] m_soundBytes;<br />
        private bool disable=false;<br />
        private enum Flags<br />
        {           <br />
            SND_ASYNC = 0x0001, <br />
            SND_MEMORY = 0x0004  <br />
        }<br />
        [DllImport("winmm.dll",CallingConvention = CallingConvention.Winapi)]<br />
        static extern int PlaySound(byte[] wfname,int hMod,int sf);<br />
        <br />
        public WSounds(string s)<br />
        {<br />
            if (s == null)<br />
            {<br />
                disable = true;<br />
                return;<br />
            }<br />
<br />
            try<br />
            {   <br />
                Stream a = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(s);<br />
                m_soundBytes = new byte[a.Length];<br />
                a.Read(m_soundBytes, 0, (int)a.Length);<br />
            }<br />
            catch (Exception e) { disable = true; }<br />
        }<br />
        public void Play()<br />
        {<br />
            if (!disable)<br />
            PlaySound(m_soundBytes, 0, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));<br />
        }<br />
    }<br />
<br />
<br />
}<br />



then in your main code you must add a file to your resources list "file.wav" for example that will be along the classes..and change his property to :"embedded resources" so it will compile him along with the exe.

and you call the class this way:

<br />
private WSounds ws;<br />
<br />
ws = new WSounds("yournamespace.file.wav");<br />
<br />
ws.Play();<br />


thats it.

R.Z

-- modified at 6:05 Monday 30th January, 2006
QuestionEncode MPEG2 Pin
Emiliano27-Jan-06 14:54
Emiliano27-Jan-06 14:54 
QuestionSliding window and always on top. Pin
RobS27-Jan-06 12:52
RobS27-Jan-06 12:52 
Questionweb services creation Pin
gregoire.b27-Jan-06 12:10
gregoire.b27-Jan-06 12:10 
GeneralRe: web services creation Pin
mast1727-Jan-06 17:53
mast1727-Jan-06 17:53 
QuestionSingle.Parse() Pin
atanamir27-Jan-06 12:05
atanamir27-Jan-06 12:05 
AnswerRe: Single.Parse() Pin
Colin Angus Mackay27-Jan-06 13:50
Colin Angus Mackay27-Jan-06 13:50 
GeneralRe: Single.Parse() Pin
atanamir27-Jan-06 15:11
atanamir27-Jan-06 15:11 
Questionapp.config file Pin
gregoire.b27-Jan-06 9:14
gregoire.b27-Jan-06 9:14 
AnswerRe: app.config file Pin
Michael P Butler27-Jan-06 10:05
Michael P Butler27-Jan-06 10:05 
Questiontype or namespace error in C#(Asp.net) Pin
idreesbadshah27-Jan-06 8:02
idreesbadshah27-Jan-06 8:02 
AnswerRe: type or namespace error in C#(Asp.net) Pin
Colin Angus Mackay28-Jan-06 5:51
Colin Angus Mackay28-Jan-06 5:51 
QuestionEvent for form has changed location Pin
Viktor Nilsson27-Jan-06 7:52
Viktor Nilsson27-Jan-06 7:52 
AnswerRe: Event for form has changed location Pin
Dave Kreskowiak27-Jan-06 8:25
mveDave Kreskowiak27-Jan-06 8:25 
AnswerRe: Event for form has changed location Pin
Robert Rohde27-Jan-06 10:43
Robert Rohde27-Jan-06 10:43 
QuestionGaussian blur troubles Pin
Eugene Kosevtsov27-Jan-06 7:12
Eugene Kosevtsov27-Jan-06 7:12 
AnswerRe: Gaussian blur troubles Pin
User 665827-Jan-06 7:38
User 665827-Jan-06 7:38 
QuestionMarquee Selection in ListView Pin
Alomgir Miah27-Jan-06 6:16
Alomgir Miah27-Jan-06 6:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.