C#播放wave提示音的类

来源:互联网 发布:淘宝网商品怎么分期购 编辑:程序博客网 时间:2024/05/19 05:03
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication7
{
    
    public class WPlaySound
    {
        [DllImport("winmm.dll", EntryPoint = "PlaySound", CharSet = CharSet.Auto)]
        private static extern int PlaySound(String pszSound, int hmod, int falgs);
        protected const int SND_SYNC = 0x0;
        protected const int SND_ASYNC = 0x1;
        protected const int SND_NODEFAULT = 0x2;
        protected const int SND_MEMORY = 0x4;
        protected const int SND_LOOP = 0x8;
        protected const int SND_NOSTOP = 0x10;
        protected const int SND_NOWAIT = 0x2000;
        protected const int SND_ALIAS = 0x10000;
        protected const int SND_ALIAS_ID = 0x110000;
        protected const int SND_FILENAME = 0x20000;
        protected const int SND_RESOURCE = 0x40004;
        protected const int SND_PURGE = 0x40;
        protected const int SND_APPLICATION = 0x80;
        /// <summary>
        ///
        /// </summary>
        public WPlaySound()
        {
        }

        /// <summary>
        /// Plays specified wav file.
        /// </summary>
        /// <param name="pszSound"></param>
        /// <returns></returns>
        public static void PlaySound(String pszSound)
        {
            // Play asynchronously
            // Asynchronously = 0x0001,
            // Name is file name
            // Filename = 0x00020000,

            if (File.Exists(pszSound))
            {
                PlaySound(pszSound, 0, SND_ASYNC | SND_FILENAME | SND_LOOP);
            }

        }
        public static void StopSound()
        {
            PlaySound(null, 0, SND_FILENAME);
        }

    }

}

可以直接调用函数,而不需要实例化对象。

0 0
原创粉丝点击