zoukankan      html  css  js  c++  java
  • C#实现在WinCE上播放声音

    此播放音效的方法几乎针对所有Windows系统都通用,废话不多说,直接上代码

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace CabbeenFactory.Service
    {
        public class VoiceService
        {
            /// <summary>
            /// 播放一个音频文件(必须是wav格式)
            /// </summary>
            /// <param name="lpszNameAs">音频文件</param>
            /// <param name="ByValhModule"></param>
            /// <param name="dwFlagsAs"></param>
            /// <returns></returns>
            [DllImport("winmm.dll", SetLastError = true, EntryPoint = "PlaySound", CharSet = CharSet.Auto)]
            private static extern int PlaySound(string lpszNameAs, int ByValhModule, int dwFlagsAs);
            private static bool PlaySoundApi(string szSound, IntPtr hModule, int flags)
            {
                int re = PlaySound(szSound, (int)hModule, flags);
                return (re != 0);
            }
    
    
            /// <summary>
            /// 异步播放一个音频文件(默认为Voice目录,必须是wav格式),播放后,不等待其完成,立即返回程序继续执行。
            /// </summary>
            /// <param name="fileName">文本:音频文件(必须是wav格式)</param>
            /// <returns>布尔:成功 True;失败 False</returns>
            public static bool PlaySoundAsync(string fileName)
            {
                return PlaySoundAsync(fileName, false);
            }
    
            /// <summary>
            /// 异步播放一个音频文件(默认为Voice目录,必须是wav格式),播放后,不等待其完成,立即返回程序继续执行。
            /// </summary>
            /// <param name="fileName">文本:音频文件(必须是wav格式)</param>
            /// <param name="Loop">布尔:是否循环播放(SND_LOOP = 8)</param>
            /// <returns>布尔:成功 True;失败 False</returns>
            public static bool PlaySoundAsync(string fileName, bool Loop) // SND_FILENAME = 131072,SND_ASYNC = 1,SND_SYNC = 0,SND_LOOP = 8
            {
                return PlaySoundAsync(fileName, Loop, 0);
            }
            /// <summary>
            /// 异步播放一个音频文件(默认为Voice目录,必须是wav格式),播放后,等待 Delayed 毫秒,返回程序继续执行。
            /// </summary>
            /// <param name="fileName">文本:音频文件(必须是wav格式)</param>
            /// <param name="Loop">布尔:是否循环播放(SND_LOOP = 8)</param>
            /// <param name="Delayed">等待毫秒</param>
            /// <returns>布尔:成功 True;失败 False</returns>
            public static bool PlaySoundAsync(string fileName, bool Loop, int Delayed) // SND_FILENAME = 131072,SND_ASYNC = 1,SND_SYNC = 0,SND_LOOP = 8
            {
                bool SoundOK = false;
                try
                {
                    //return PlaySound(fileName, IntPtr.Zero, (int)PlaySoundFlags.SND_FILENAME);131072
    
                    if (Loop)
                    {
                        SoundOK = PlaySoundApi("声音文件路径", IntPtr.Zero, 131081);
                    }
                    else
                    {
                        SoundOK = PlaySoundApi("声音文件路径", IntPtr.Zero, 131073);
                    }
                    if (Delayed > 0)
                    {
                        System.Threading.Thread.Sleep(Delayed);
                    }
                }
                catch (System.Exception ex)
                {
                    //MessageBox.Show(ex.Message, "播放音频");
                    return false;
                }
                return SoundOK;
            }
    
            /// <summary>
            /// 播放成功提示音
            /// </summary>
            public static void Success()
            {
                PlaySoundAsync("success.wav", false, 800);
            }
    
            /// <summary>
            /// 播放错误提示音
            /// </summary>
            public static void Error()
            {
                PlaySoundAsync("wrong.wav", false, 800);
            }
        }
    }
  • 相关阅读:
    Binary Tree Maximum Path Sum
    ZigZag Conversion
    Longest Common Prefix
    Reverse Linked List II
    Populating Next Right Pointers in Each Node
    Populating Next Right Pointers in Each Node II
    Rotate List
    Path Sum II
    [Leetcode]-- Gray Code
    Subsets II
  • 原文地址:https://www.cnblogs.com/mooncher/p/14499930.html
Copyright © 2011-2022 走看看