⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 waveout.cs

📁 本人觉得C#的软件是不需要源代码的
💻 CS
📖 第 1 页 / 共 2 页
字号:
using Microsoft.WindowsCE.Forms;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace SmartAnswerCall.处理类
{



    public class WaveOut : IDisposable
    {
        protected WaveFile m_file = null;
        protected SoundMessageWindow m_msgWindow = null;
        protected const uint TIME_BYTES = 4;
        protected const uint TIME_MS = 1;
        protected const uint TIME_SAMPLES = 2;
        protected const uint TIME_TICKS = 0x20;

        public WaveOut()
        {
            this.m_msgWindow = new SoundMessageWindow(this);
            this.m_file = new WaveFile();
        }

        public void BlockDone(IntPtr hwo)
        {
            if (this.m_file != null)
            {
                this.m_file.BlockDone();
            }
        }

        public void Dispose()
        {
            if (this.m_msgWindow != null)
            {
                this.m_msgWindow.Dispose();
            }
            if (this.m_file != null)
            {
                this.m_file.Dispose();
            }
        }

        public bool Done()
        {
            if (this.m_file != null)
            {
                return this.m_file.Done;
            }
            return true;
        }

        public Wave.MMSYSERR GetDeviceName(uint deviceId, ref string prodName)
        {
            WAVEOUTCAPS waveoutcaps = new WAVEOUTCAPS();
            Wave.MMSYSERR mmsyserr = waveOutGetDevCaps(deviceId, (byte[])waveoutcaps, waveoutcaps.Size);
            if (mmsyserr != Wave.MMSYSERR.NOERROR)
            {
                return mmsyserr;
            }
            prodName = waveoutcaps.szPname;
            return Wave.MMSYSERR.NOERROR;
        }

        public uint Milliseconds()
        {
            if (this.m_file != null)
            {
                return this.m_file.Milliseconds;
            }
            return 0;
        }

        public uint NumDevices()
        {
            return (uint)waveOutGetNumDevs();
        }

        public Wave.MMSYSERR Pause()
        {
            if (this.m_file != null)
            {
                return this.m_file.Pause();
            }
            return Wave.MMSYSERR.NOERROR;
        }

        public Wave.MMSYSERR Play(string fileName, int bufferSize, ushort volLeft, ushort volRight)
        {
            if (this.m_file != null)
            {
                return this.m_file.Play(0, fileName, this.m_msgWindow.Hwnd, bufferSize, volLeft, volRight);
            }
            return Wave.MMSYSERR.ERROR;
        }

        public Wave.MMSYSERR Resume()
        {
            if (this.m_file != null)
            {
                return this.m_file.Resume();
            }
            return Wave.MMSYSERR.NOERROR;
        }

        public static void TestProc(MainTest.DisplayLineDelegate showLine)
        {
            WaveOut @out = null;
            using (@out = new WaveOut())
            {
                uint num = @out.NumDevices();
                if (num < 1)
                {
                    showLine("FAILURE: No valid sound drivers detected");
                }
                else
                {
                    showLine(string.Format("{0} device{1} detected:", num, (num != 1) ? "s" : ""));
                    for (uint i = 0; i < num; i++)
                    {
                        string prodName = "";
                        if (@out.GetDeviceName(i, ref prodName) != Wave.MMSYSERR.NOERROR)
                        {
                            showLine(string.Format(" {0}: Failed to get name", i));
                        }
                        else
                        {
                            showLine(string.Format(" {0}: {1}", i, prodName));
                        }
                    }
                    string fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "test.wav");
                    showLine("Playing sound test.wav");
                    showLine("Setting stream buffers to 512KB total");
                    if (@out.Play(fileName, 0x80000, 0xffff, 0xffff) != Wave.MMSYSERR.NOERROR)
                    {
                        showLine("FAILURE: Failed to play sound test.wav");
                    }
                    int num4 = (int)(((float)@out.Milliseconds()) / 3f);
                    showLine(string.Format("Sleeping for {0} ms", num4));
                    Thread.Sleep(num4);
                    showLine("Pausing for 1 second...");
                    @out.Pause();
                    Thread.Sleep(0x3e8);
                    showLine("Resuming...");
                    @out.Resume();
                    showLine("Waiting for sound to finish...");
                    num4 = 5;
                    int num5 = 0;
                    int num6 = (int)(((ulong)@out.Milliseconds())/((ulong)num4));
                    while (!@out.Done())
                    {
                        Thread.Sleep(num4);
                        Application.DoEvents();
                        num5++;
                        if (num5 > num6)
                        {
                            showLine("FAILURE: Failed to detecte end of sound");
                            return;
                        }
                    }
                    showLine("Done playing sound");
                }
            }
        }

        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutClose(IntPtr hwo);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutGetDevCaps(uint uDeviceID, byte[] pwoc, uint cbwoc);
        [DllImport("coredll.dll")]
        protected static extern int waveOutGetNumDevs();
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutGetPosition(IntPtr hwo, MMTIME pmmt, uint cbmmt);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutGetVolume(IntPtr hwo, ref uint pdwVolume);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveOutOpen(ref IntPtr phwo, uint uDeviceID, Wave.WAVEFORMATEX pwfx, IntPtr dwCallback, uint dwInstance, uint fdwOpen);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutPause(IntPtr hwo);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveOutPrepareHeader(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutReset(IntPtr hwo);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutRestart(IntPtr hwo);
        [DllImport("coredll.dll")]
        protected static extern Wave.MMSYSERR waveOutSetVolume(IntPtr hwo, uint dwVolume);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveOutUnprepareHeader(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);
        [DllImport("coredll.dll")]
        private static extern Wave.MMSYSERR waveOutWrite(IntPtr hwo, Wave.WAVEHDR pwh, uint cbwh);

        protected class MMTIME
        {
            public uint cb = 0;
            public uint pad = 0;
            public uint wType = 0;
        }

        public class SoundMessageWindow : MessageWindow
        {
            protected WaveOut m_wo = null;
            public const int MM_WOM_CLOSE = 0x3bc;
            public const int MM_WOM_DONE = 0x3bd;
            public const int MM_WOM_FORCESPEAKER = 0x402;
            public const int MM_WOM_OPEN = 0x3bb;
            public const int MM_WOM_SETSECONDARYGAINCLASS = 0x400;
            public const int MM_WOM_SETSECONDARYGAINLIMIT = 0x401;
            public const int WM_USER = 0x400;

            public SoundMessageWindow(WaveOut wo)
            {
                this.m_wo = wo;
            }

            protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)
            {
                if (msg.Msg == 0x3bd)
                {
                    this.m_wo.BlockDone(msg.WParam);
                }
                base.WndProc(ref msg);
            }
        }

        public sealed class SpeakerPhone
        {
            private SpeakerPhone()
            {
            }

            public static void Toggle()
            {
                NativeMethods.keybd_event(0x7f, 0, 0, 0);
                NativeMethods.keybd_event(0x7f, 0, 2, 0);
            }

            internal class NativeMethods
            {
                internal const int KEYEVENTF_KEYUP = 2;

                [DllImport("coredll.dll")]
                internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
            }
        }

        public class WaveFile : IDisposable
        {
            protected int m_bufferSize;
            protected int m_curBlock;
            protected uint m_dataLength;
            protected IntPtr m_hwo = IntPtr.Zero;
            protected int m_numBlocks;
            protected bool m_playing = false;
            protected BinaryReader m_rdr = null;
            protected Wave.WAVEFORMATEX m_wfmt = null;
            protected Wave.WAVEHDR[] m_whdr = null;

            public WaveFile()
            {
                this.m_whdr = new Wave.WAVEHDR[2];
            }

            public void BlockDone()
            {
                this.m_curBlock++;
                if (this.m_curBlock < this.m_numBlocks)
                {
                    new Thread(new ThreadStart(this.LoadBuffer)).Start();
                }
                else if (this.m_curBlock == this.m_numBlocks)
                {
                    this.Stop();
                }
            }

            protected Wave.MMSYSERR CreateBuffer(int bufIndex)
            {
                Wave.MMSYSERR mmsyserr = this.m_whdr[bufIndex].Init((uint)this.m_bufferSize, true);
                if (mmsyserr != Wave.MMSYSERR.NOERROR)
                {
                    return mmsyserr;
                }
                if ((this.m_whdr[bufIndex].dwFlags & 2) == 0)
                {
                    return WaveOut.waveOutPrepareHeader(this.m_hwo, this.m_whdr[bufIndex], (uint)Marshal.SizeOf(this.m_whdr[bufIndex]));
                }
                return Wave.MMSYSERR.NOERROR;
            }

            public void Dispose()
            {
                this.Stop();
            }

            public Wave.MMSYSERR GetVolume(ref ushort volLeft, ref ushort volRight)
            {
                uint pdwVolume = 0;
                Wave.MMSYSERR mmsyserr = WaveOut.waveOutGetVolume(this.m_hwo, ref pdwVolume);
                if (mmsyserr != Wave.MMSYSERR.NOERROR)
                {
                    return mmsyserr;
                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -