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);
            }
        }
    }
  • 相关阅读:
    android ndk通过遍历和删除文件
    SVN提交忽略*.class、.classpath、.mymetadata、.project、.settings、.myeclipse和其他非版本控制文件
    JDBC加载过程
    JVMTI 中间JNI系列功能,线程安全和故障排除技巧
    【Python】Python与文本处理langid工具包的文本语言检测和歧视
    SQL 存储过程 分页
    Android:仿手机QQ朋友动态ListView
    再说Java EE
    辛星与您解读PHP页面跳转的几种实现方式
    VS2010 使用TeeChart画图控件
  • 原文地址:https://www.cnblogs.com/mooncher/p/14499930.html
Copyright © 2011-2022 走看看