none
Play sound when button is pressed

    Question

  • I would like my application to play a

    sound when i press button...

    How do I do this?

    Monday, September 13, 2010 12:51 PM

Answers

  • Use the SoundPlayer class.

    For Windows' system sounds (like Beep or Question), use SystemSounds


    Mark the best replies as answers!
    • Marked as answer by RokoLAB Monday, September 13, 2010 3:04 PM
    Monday, September 13, 2010 1:27 PM
  • Small clarification, the SoundPlayer class will only work if the file is a wav. If so fine, else you will need COM

    Regards

    • Marked as answer by RokoLAB Monday, September 13, 2010 3:04 PM
    Monday, September 13, 2010 2:02 PM
  • Here my class. Year or two ago I needed function of playing audio in my project, so i had to write it.

    There are more functions then author need such as volume regulator and others.

     

    public class Player
     {
      private string Pcommand;
      private bool isOpen, isPlaying, ispaused;
      private ulong Lng;
      [DllImport("winmm.dll")]
      private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
      private static Player player;
      private int baseVolumn;
      private int trebleVolumn;
      private int leftVolumn;
      private int rightVolumn;
      private int masterVolumn;
      private int speed;
      private float lCoeff, rCoeff;
      private int volBalance; //-1000 .. 1000
      public Player()
      {
       this.Bass = 5 * 100;
       this.LeftVolume = 5 * 100;
       this.MasterVolume = 5 * 100;
       this.RightVolume = 5 * 100;
       this.Treble = 5 * 100;
       this.volBalance = 0;
       this.speed = 1000;
       lCoeff = 1;
       rCoeff = 1;
      }
      #region Methods
    
      public static Player GetPlayer()
      {
       if (player == null)
        player = new Player();
       return player;
      }
      public void Close()
      {
       Pcommand = "close MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       isOpen = false;
       isPlaying = false;
       ispaused = false;
      }
    
      public void Open(string sFileName)
      {
       Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       Pcommand = "set MediaFile time format milliseconds";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       Pcommand = "set MediaFile seek exactly on";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       CalculateLength();
       isOpen = true;
      }
    
      private void CalculateLength()
      {
       StringBuilder str = new StringBuilder(128);
       mciSendString("status MediaFile length", str, 128, IntPtr.Zero);
       if (str.Length != 0)
        Lng = Convert.ToUInt64(str.ToString());
       else
        Lng = 0;
      }
    
      public ulong PlayLength
      {
       get { return Lng; }
      }
    
    
      public void Play(bool loop)
      {
       if (isOpen)
       {
        Pcommand = "play MediaFile";
        if (loop)
         Pcommand += " REPEAT";
        mciSendString(Pcommand, null, 0, IntPtr.Zero);
        this.Bass = this.Bass;
        this.Treble = this.Treble;
        //this.LeftVolume = this.LeftVolume;
        //this.RightVolume = this.RightVolume;
        this.MasterVolume = this.MasterVolume;
        isPlaying = true;
       }
      }
      public void Play(string FileName)
      {
       if (isOpen == true)
        Close();
       Open(FileName);
       Play(false);
      }
      public bool IsPlaying()
      {
       return isPlaying;
      }
      public void Stop()
      {
       this.Close();
      }
      public void Pause()
      {
       ispaused = true;
       Pcommand = "pause MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
      }
      public bool IsPaused()
      {
       return ispaused;
      }
      public void Resume()
      {
       Pcommand = "resume MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       ispaused = false;
       isPlaying = true;
      }
      public string Status()
      {
       int i = 128;
       System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
       mciSendString("status MediaFile mode", stringBuilder, i, IntPtr.Zero);
       return stringBuilder.ToString();
      }
    
      /// <summary>
      /// Speed of playing
      /// </summary>
      /// <param name="speed"></param>
      public void SetSpeed(int speed)
      {
       this.speed = speed;
       Pcommand = string.Format("set MediaFile speed {0}", speed);
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
      }
    
      public void Seek(ulong Millisecs)
      {
       if (isOpen && Millisecs <= Lng)
       {
        if (isPlaying)
        {
         if (IsPaused())
         {
          Pcommand = String.Format("seek MediaFile to {0}", Millisecs);
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
         }
         else
         {
          Pcommand = String.Format("seek MediaFile to {0}", Millisecs);
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
          Pcommand = "play MediaFile";
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
         }
        }
       }
      }
      public int CurrentSpeed
      {
       get
       {
        return speed;
       }
       set
       {
        SetSpeed(value);
       }
      }
    
      public ulong CurrentPosition
      {
       get
       {
        if (isOpen && isPlaying)
        {
         StringBuilder s = new StringBuilder(128);
         Pcommand = "status MediaFile position";
         mciSendString(Pcommand, s, 128, IntPtr.Zero);
         if (s.Length != 0)
          return Convert.ToUInt64(s.ToString());
         else
          return 0;
        }
        else return 0;
       }
       set
       {
        if (value <= PlayLength)
         mciSendString("play MediaFile from " + value.ToString(), null, 0, IntPtr.Zero);
       }
      }
    
      #endregion
    
      #region Properties
      public int Bass
      {
       get
       {
        return baseVolumn;
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         mciSendString(string.Concat("setaudio MediaFile bass to ", value), null, 0, IntPtr.Zero);
         baseVolumn = value;
        }
       }
      }
      public int Treble
      {
       get
       {
        return trebleVolumn;
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         mciSendString(string.Concat("setaudio MediaFile treble to ", value), null, 0, IntPtr.Zero);
         trebleVolumn = value;
        }
       }
      }
      public int Balance
      {
       get
       {
        return volBalance;
       }
       set
       {
        if (value >= -1000 && value <= 1000)
        {
         volBalance = value;
         if (value < 0)
         {
          lCoeff = 1f;
          rCoeff = (float)(1000 + value) / 1000f;
         }
         else
         {
          lCoeff = (float)(1000 - value) / 1000f;
          rCoeff = 1f;
         }
         leftVolumn = (int)(value * lCoeff);
         rightVolumn = (int)(value * rCoeff);
         MasterVolume = masterVolumn;
        }
       }
      }
      public int MasterVolume
      {
       get
       {
        return masterVolumn; //Guess could be used to return Volume level: I don't need it
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         leftVolumn = (int)(value * lCoeff);
         rightVolumn = (int)(value * rCoeff);
         mciSendString(string.Concat("setaudio MediaFile left volume to ", value * lCoeff), null, 0, IntPtr.Zero);
         mciSendString(string.Concat("setaudio MediaFile right volume to ", value * rCoeff), null, 0, IntPtr.Zero);
         //mciSendString(string.Concat("setaudio MediaFile volume to ", value), null, 0, IntPtr.Zero);
         masterVolumn = value;
        }
       }
      }
      public int RightVolume
      {
       get
       {
        return rightVolumn; 
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         rCoeff = (float)value / 1000f;
         mciSendString(string.Concat("setaudio MediaFile right volume to ", value), null, 0, IntPtr.Zero);
         rightVolumn = value;
        }
       }
      }
      public int LeftVolume
      {
       get
       {
        return leftVolumn; 
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         lCoeff = (float)value / 1000f;
         mciSendString(string.Concat("setaudio MediaFile left volume to ", value), null, 0, IntPtr.Zero);
         leftVolumn = value;
        }
       }
      }
      #endregion
     }

    To play audio with this class you have to do next

    Player player = new Player();
    
     player.Play("c:\\Path Vol.mp3"); // here put your audio filename
    

     

    PS: Terrible designer

     

    • Edited by Syav4eg Monday, September 13, 2010 2:48 PM
    • Marked as answer by RokoLAB Monday, September 13, 2010 3:05 PM
    Monday, September 13, 2010 2:26 PM

All replies

  • It depends friend on what type of sound, if is a beep, it is easy because that is a key, look it up in an ascii table.

    If not you might need to use a COM object such as the windows media player, you can add it using the toolbox

    Regards

    Monday, September 13, 2010 1:16 PM
  • Use the SoundPlayer class.

    For Windows' system sounds (like Beep or Question), use SystemSounds


    Mark the best replies as answers!
    • Marked as answer by RokoLAB Monday, September 13, 2010 3:04 PM
    Monday, September 13, 2010 1:27 PM
  • Small clarification, the SoundPlayer class will only work if the file is a wav. If so fine, else you will need COM

    Regards

    • Marked as answer by RokoLAB Monday, September 13, 2010 3:04 PM
    Monday, September 13, 2010 2:02 PM
  • Small clarification, the SoundPlayer class will only work if the file is a wav. If so fine, else you will need COM

    Regards


    COM ?!

    can u put sample ?!

    Monday, September 13, 2010 2:05 PM
  • Ahh, thinking back, is easier to use this or something close

    http://msdn.microsoft.com/en-us/library/dd562692(VS.85).aspx

    Regards

    • Proposed as answer by deadManN Monday, September 13, 2010 2:21 PM
    Monday, September 13, 2010 2:20 PM
  • tnx so much
    Monday, September 13, 2010 2:21 PM
  • Here my class. Year or two ago I needed function of playing audio in my project, so i had to write it.

    There are more functions then author need such as volume regulator and others.

     

    public class Player
     {
      private string Pcommand;
      private bool isOpen, isPlaying, ispaused;
      private ulong Lng;
      [DllImport("winmm.dll")]
      private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
      private static Player player;
      private int baseVolumn;
      private int trebleVolumn;
      private int leftVolumn;
      private int rightVolumn;
      private int masterVolumn;
      private int speed;
      private float lCoeff, rCoeff;
      private int volBalance; //-1000 .. 1000
      public Player()
      {
       this.Bass = 5 * 100;
       this.LeftVolume = 5 * 100;
       this.MasterVolume = 5 * 100;
       this.RightVolume = 5 * 100;
       this.Treble = 5 * 100;
       this.volBalance = 0;
       this.speed = 1000;
       lCoeff = 1;
       rCoeff = 1;
      }
      #region Methods
    
      public static Player GetPlayer()
      {
       if (player == null)
        player = new Player();
       return player;
      }
      public void Close()
      {
       Pcommand = "close MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       isOpen = false;
       isPlaying = false;
       ispaused = false;
      }
    
      public void Open(string sFileName)
      {
       Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       Pcommand = "set MediaFile time format milliseconds";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       Pcommand = "set MediaFile seek exactly on";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       CalculateLength();
       isOpen = true;
      }
    
      private void CalculateLength()
      {
       StringBuilder str = new StringBuilder(128);
       mciSendString("status MediaFile length", str, 128, IntPtr.Zero);
       if (str.Length != 0)
        Lng = Convert.ToUInt64(str.ToString());
       else
        Lng = 0;
      }
    
      public ulong PlayLength
      {
       get { return Lng; }
      }
    
    
      public void Play(bool loop)
      {
       if (isOpen)
       {
        Pcommand = "play MediaFile";
        if (loop)
         Pcommand += " REPEAT";
        mciSendString(Pcommand, null, 0, IntPtr.Zero);
        this.Bass = this.Bass;
        this.Treble = this.Treble;
        //this.LeftVolume = this.LeftVolume;
        //this.RightVolume = this.RightVolume;
        this.MasterVolume = this.MasterVolume;
        isPlaying = true;
       }
      }
      public void Play(string FileName)
      {
       if (isOpen == true)
        Close();
       Open(FileName);
       Play(false);
      }
      public bool IsPlaying()
      {
       return isPlaying;
      }
      public void Stop()
      {
       this.Close();
      }
      public void Pause()
      {
       ispaused = true;
       Pcommand = "pause MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
      }
      public bool IsPaused()
      {
       return ispaused;
      }
      public void Resume()
      {
       Pcommand = "resume MediaFile";
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
       ispaused = false;
       isPlaying = true;
      }
      public string Status()
      {
       int i = 128;
       System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
       mciSendString("status MediaFile mode", stringBuilder, i, IntPtr.Zero);
       return stringBuilder.ToString();
      }
    
      /// <summary>
      /// Speed of playing
      /// </summary>
      /// <param name="speed"></param>
      public void SetSpeed(int speed)
      {
       this.speed = speed;
       Pcommand = string.Format("set MediaFile speed {0}", speed);
       mciSendString(Pcommand, null, 0, IntPtr.Zero);
      }
    
      public void Seek(ulong Millisecs)
      {
       if (isOpen && Millisecs <= Lng)
       {
        if (isPlaying)
        {
         if (IsPaused())
         {
          Pcommand = String.Format("seek MediaFile to {0}", Millisecs);
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
         }
         else
         {
          Pcommand = String.Format("seek MediaFile to {0}", Millisecs);
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
          Pcommand = "play MediaFile";
          mciSendString(Pcommand, null, 0, IntPtr.Zero);
         }
        }
       }
      }
      public int CurrentSpeed
      {
       get
       {
        return speed;
       }
       set
       {
        SetSpeed(value);
       }
      }
    
      public ulong CurrentPosition
      {
       get
       {
        if (isOpen && isPlaying)
        {
         StringBuilder s = new StringBuilder(128);
         Pcommand = "status MediaFile position";
         mciSendString(Pcommand, s, 128, IntPtr.Zero);
         if (s.Length != 0)
          return Convert.ToUInt64(s.ToString());
         else
          return 0;
        }
        else return 0;
       }
       set
       {
        if (value <= PlayLength)
         mciSendString("play MediaFile from " + value.ToString(), null, 0, IntPtr.Zero);
       }
      }
    
      #endregion
    
      #region Properties
      public int Bass
      {
       get
       {
        return baseVolumn;
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         mciSendString(string.Concat("setaudio MediaFile bass to ", value), null, 0, IntPtr.Zero);
         baseVolumn = value;
        }
       }
      }
      public int Treble
      {
       get
       {
        return trebleVolumn;
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         mciSendString(string.Concat("setaudio MediaFile treble to ", value), null, 0, IntPtr.Zero);
         trebleVolumn = value;
        }
       }
      }
      public int Balance
      {
       get
       {
        return volBalance;
       }
       set
       {
        if (value >= -1000 && value <= 1000)
        {
         volBalance = value;
         if (value < 0)
         {
          lCoeff = 1f;
          rCoeff = (float)(1000 + value) / 1000f;
         }
         else
         {
          lCoeff = (float)(1000 - value) / 1000f;
          rCoeff = 1f;
         }
         leftVolumn = (int)(value * lCoeff);
         rightVolumn = (int)(value * rCoeff);
         MasterVolume = masterVolumn;
        }
       }
      }
      public int MasterVolume
      {
       get
       {
        return masterVolumn; //Guess could be used to return Volume level: I don't need it
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         leftVolumn = (int)(value * lCoeff);
         rightVolumn = (int)(value * rCoeff);
         mciSendString(string.Concat("setaudio MediaFile left volume to ", value * lCoeff), null, 0, IntPtr.Zero);
         mciSendString(string.Concat("setaudio MediaFile right volume to ", value * rCoeff), null, 0, IntPtr.Zero);
         //mciSendString(string.Concat("setaudio MediaFile volume to ", value), null, 0, IntPtr.Zero);
         masterVolumn = value;
        }
       }
      }
      public int RightVolume
      {
       get
       {
        return rightVolumn; 
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         rCoeff = (float)value / 1000f;
         mciSendString(string.Concat("setaudio MediaFile right volume to ", value), null, 0, IntPtr.Zero);
         rightVolumn = value;
        }
       }
      }
      public int LeftVolume
      {
       get
       {
        return leftVolumn; 
       }
       set
       {
        if (value >= 0 && value <= 1000)
        {
         lCoeff = (float)value / 1000f;
         mciSendString(string.Concat("setaudio MediaFile left volume to ", value), null, 0, IntPtr.Zero);
         leftVolumn = value;
        }
       }
      }
      #endregion
     }

    To play audio with this class you have to do next

    Player player = new Player();
    
     player.Play("c:\\Path Vol.mp3"); // here put your audio filename
    

     

    PS: Terrible designer

     

    • Edited by Syav4eg Monday, September 13, 2010 2:48 PM
    • Marked as answer by RokoLAB Monday, September 13, 2010 3:05 PM
    Monday, September 13, 2010 2:26 PM
  • i think i take a copy of this, i like hard way, they are hard and complex, but they can take any form...

    they are not limit to what author want from the file...

    Monday, September 13, 2010 2:34 PM