[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 893
  • Last Modified:

What cause my C#.NET program to mute sound??

Here is the Sound object someone wrote for me on EE that works great, but causes the Windows sound to mute, effectively halting my program's usefulness.

    public class Sound
      {
            private byte[] m_soundBytes;
            private string m_fileName;        

            private enum Flags
            {
                  SND_SYNC = 0x0000,  /* play synchronously (default) */
                  SND_ASYNC = 0x0001,  /* play asynchronously */
                  SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
                  SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
                  SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
                  SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
                  SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
                  SND_ALIAS = 0x00010000, /* name is a registry alias */
                  SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
                  SND_FILENAME = 0x00020000, /* name is file name */
                  SND_RESOURCE = 0x00040004  /* name is resource name or atom */
            }

            /*[DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]
            private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);

            [DllImport("CoreDll.DLL", EntryPoint="PlaySound", SetLastError=true)]
            private extern static int WCE_PlaySoundBytes (byte[] szSound, IntPtr hMod, int flags);*/

            [DllImport("winmm.DLL", EntryPoint="PlaySound", SetLastError=true)]
            private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);

            [DllImport("winmm.DLL", EntryPoint="PlaySound", SetLastError=true)]
            private extern static int WCE_PlaySoundBytes (byte[] szSound, IntPtr hMod, int flags);

            /// <summary>
            /// Construct the Sound object to play sound data from the specified file.
            /// </summary>
            public Sound (string fileName)
            {
                  m_fileName = fileName;
            }

            /// <summary>
            /// Construct the Sound object to play sound data from the specified stream.
            /// </summary>
            public Sound(Stream stream)    
            {
                  // read the data from the stream
                  m_soundBytes = new byte [stream.Length];
                  stream.Read(m_soundBytes, 0,(int)stream.Length);
            }

            /// <summary>
            /// Play the sound
            /// </summary>
            public void Play (Handles handles)
            {
            if (handles.AdjustVolume != null)
            {
                handles.AdjustVolume.SetVolume();
            }
                  // if a file name has been registered, call WCE_PlaySound,
                  //  otherwise call WCE_PlaySoundBytes
                  if (m_fileName != null)
                        WCE_PlaySound(m_fileName, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_FILENAME));
                  else
                        WCE_PlaySoundBytes (m_soundBytes, IntPtr.Zero, (int) (Flags.SND_ASYNC | Flags.SND_MEMORY));
            }

        public static void PlaySound(Handles handles, string fileName)
            {
                  Sound sound = new Sound( fileName );
            sound.Play(handles);
            }

        public static void RingBell( Handles handles, string soundName, int numRings, Condition condition )
        {
            RingBellTimer ringBellTimer = new RingBellTimer(handles, soundName);
            TimeSpan timeSpan = new TimeSpan(0, 0, 15);
            handles.TaskQueue.Add(new RepeatingFiniteTask(handles, ringBellTimer, timeSpan, numRings, condition));
        }
}

Does anyone have an idea why???  I urgently need a solution.

thanks,
newbieweb
0
newbieweb
Asked:
newbieweb
  • 6
  • 6
1 Solution
 
SolutionsCSCommented:
(int) (Flags.SND_ASYNC or Flags.SND_NOSTOP) could help if a sound currently play.
But the method used here won't permit mixing with orther sound source.
try it instead... ;)

My.Computer.Audio.Play("Your sound file")
0
 
newbiewebSr. Software EngineerAuthor Commented:
I do not know how to access My.Computer.Audio.Play from within C#.NET.

Do you know??
0
 
newbiewebSr. Software EngineerAuthor Commented:
No, the addition of Flags.SND_NOSTOP did not help.  I already had Flags.SND_ASYNC.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SolutionsCSCommented:
System.io.stream


see that reference.
http://msdn2.microsoft.com/en-us/library/cf1shcah.aspx
0
 
newbiewebSr. Software EngineerAuthor Commented:
In C# there is not System.io.stream.

And I don't know VB.  Can you suggest anything else I can try?
0
 
SolutionsCSCommented:
This also exist in c# if you look at the link i sent you

http://msdn2.microsoft.com/en-us/library/cf1shcah.aspx
0
 
SolutionsCSCommented:
Here is an alternative for you in c#
  public class WSounds
  {
    [DllImport("WinMM.dll")]
    public static extern bool  PlaySound(string fname, int Mod, int flag);
 
    // these are the SoundFlags we are using here, check mmsystem.h for more
    public int SND_ASYNC    = 0x0001;     // play asynchronously
    public int SND_FILENAME = 0x00020000; // use file name
    public int SND_PURGE    = 0x0040;     // purge non-static events
 
    public void Play(string fname, int SoundFlags)
    {
      PlaySound(fname, 0, SoundFlags);
    }
 
    public void StopPlay()
    {
      PlaySound(null, 0, SND_PURGE);
    }
  }
 
}

Open in new window

0
 
newbiewebSr. Software EngineerAuthor Commented:
When I call Play(), what do I use as a SoundFlags value??
0
 
newbiewebSr. Software EngineerAuthor Commented:
Do yo ukonw the Hex value for Flags.SND_NOSTOP??  maybe it's not defined in my object which is why it did not work??
0
 
SolutionsCSCommented:
SND_ASYNC, as before
0
 
SolutionsCSCommented:
oups and SND_FILENAME Too
SND_ASYNC or SND_ASYNC

0
 
newbiewebSr. Software EngineerAuthor Commented:
I do have the value for that SND_NOSTOP: 0x0010

But when I run the program it doews not override the Mute All checkbox.

Any ideas?  This is a real problem. Ay idea what could cause the Mute All to check itself automatically anyway??
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now