using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace ryCommon
{
    /// <summary>
    /// 音频播放类
    /// </summary>
    public class SoundPlay
    {
        [DllImport("winmm.dll", SetLastError = true)]
        static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);
        [DllImport("winmm.dll")]
        static extern Int32 mciSendString(String command, StringBuilder buffer, Int32 bufferSize, IntPtr hwndCallback);
        /// <summary>
        /// 临时音频文件
        /// </summary>
        private String m_musicPath = "";
        /// <summary>
        ///父窗口句柄
        /// </summary>
        private IntPtr m_Handle;
        /// <summary>
        /// 声音标签
        /// </summary>
        [Flags]
        public enum SoundFlags
        {
            /// <summary>play synchronously (default)</summary>
            SND_SYNC = 0x0000,
            /// <summary>play asynchronously</summary>
            SND_ASYNC = 0x0001,
            /// <summary>silence (!default) if sound not found</summary>
            SND_NODEFAULT = 0x0002,
            /// <summary>pszSound points to a memory file</summary>
            SND_MEMORY = 0x0004,
            /// <summary>loop the sound until next sndPlaySound</summary>
            SND_LOOP = 0x0008,
            /// <summary>don’t stop any currently playing sound</summary>
            SND_NOSTOP = 0x0010,
            /// <summary>Stop Playing Wave</summary>
            SND_PURGE = 0x40,
            /// <summary>don’t wait if the driver is busy</summary>
            SND_NOWAIT = 0x00002000,
            /// <summary>name is a registry alias</summary>
            SND_ALIAS = 0x00010000,
            /// <summary>alias is a predefined id</summary>
            SND_ALIAS_ID = 0x00110000,
            /// <summary>name is file name</summary>
            SND_FILENAME = 0x00020000,
            /// <summary>name is resource name or atom</summary>
            SND_RESOURCE = 0x00040004
        }
        /// <summary>
        /// 音频播放类
        /// </summary>
        /// <param name="Music">embedded music file</param>
        /// <param name="folder_path">临时保存的文件位置</param>
        /// <param name="Handle">父窗口句柄</param>
        public SoundPlay(Byte[] Music, string folder_path, IntPtr Handle)
        {
            try
            {
                m_Handle = Handle;
                m_musicPath = Path.Combine(folder_path, "temp.mp3");
                FileStream fs = new FileStream(m_musicPath, FileMode.Create);
                fs.Write(Music, 0, Music.Length);
                fs.Close();
            }
            catch (Exception)
            {
            }
        }
        /// <summary>
        ///音频播放类
        /// </summary>
        /// <param name="musicPath">要播放的音频位置</param>
        /// <param name="Handle">父窗口句柄</param>
        public SoundPlay(String musicPath, IntPtr Handle)
        {
            m_musicPath = musicPath;
            m_Handle = Handle;
        }
        /// <summary>
        /// 音频播放类
        /// </summary>
        /// <param name="Music"></param>
        /// <param name="Handle">父窗口句柄</param>
        public SoundPlay(Byte[] Music, IntPtr Handle) : this(Music, @"C:\Windows\",Handle)
        {
        }
        [DllImport("Kernel32", CharSet = CharSet.Auto)]
        static extern Int32 GetShortPathName(String path, StringBuilder shortPath, Int32 shortPathLength);
        /// <summary>
        /// 播放音频
        /// </summary>
        /// <param name="path"></param>
        public void Play(string path)
        {
           if(path != "" && System.IO.File.Exists(path))
          {
                try
                {
                    string ext = System.IO.Path.GetExtension(path).ToLower();
                    if (ext == ".mp3")
                    {
                        StringBuilder shortpath = new StringBuilder(260);
                        int result = GetShortPathName(path, shortpath, shortpath.Capacity);
                        mciSendString(@"close all", null, 0, m_Handle);
                        mciSendString("open " + shortpath.ToString() + " alias song", null, 0, m_Handle);
                        mciSendString("play song", null, 0, m_Handle);
                    }
                    else if (ext == ".wav")
                    {
                        PlaySound(path, m_Handle,(uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC | SoundFlags.SND_NOSTOP));

                    }
                }
                catch (Exception)
                {
                }
            }
        }
        /// <summary>
        /// 播放音频
        /// </summary>
        public void Play()
        {
            Play(m_musicPath);
        }
        /// <summary>
        /// 关闭音频
        /// </summary>
        void CloseMedia()
        {
            try
            {
                mciSendString("Close ALL", null, 0, m_Handle);
            }
            catch (Exception)
            {
            }
        }
    }
}
