示例#1
0
        public int Play()
        {
            if (!this.isInit)
            {
                return(LibErrors.NOT_INITIALIZED);
            }

            if (this.stream == null)
            {
                return(LibErrors.STREAM_NOT_OPENED);
            }

            this.dsb8.SetCurrentPosition(0);
            uint dsErr = this.dsb8.Play(0, 0, DSBPLAY.DSBPLAY_LOOPING);

            if (dsErr != DSERR.DS_OK)
            {
                LibUtils.PrintLog("Play失败, DSERR = {0}", dsErr);
                return(LibErrors.DS_ERROR);
            }

            this.isPlaying = true;

            this.NotifyStatusChanged(DSLibPlayerStatus.Playing, 0);

            uint offset = (uint)LibConsts.BUFF_NOTIFY_SIZE;

            Task.Factory.StartNew((state) =>
            {
                while (this.isPlaying)
                {
                    IntPtr lpHandles = Marshal.UnsafeAddrOfPinnedArrayElement(this.notifyHwnd_close, 0);

                    uint notifyIdx = LibNatives.WaitForMultipleObjects(LibConsts.BUFF_NOTIFY_TIMES, lpHandles, false, LibNatives.INFINITE);
                    if ((notifyIdx >= LibNatives.WAIT_OBJECT_0) && (notifyIdx <= LibNatives.WAIT_OBJECT_0 + LibConsts.BUFF_NOTIFY_TIMES))
                    {
                        if (!this.isPlaying)
                        {
                            // 通知是异步的,在调用了Stop之后, 如果收到通知的速度比音频流Seek(0)的速度慢(也就是说先重置了音频流,然后又收到了一次通知), 有可能会再次读取一次数据
                            break;
                        }

                        this.HandleNotification(offset, LibConsts.BUFF_NOTIFY_SIZE, state as SynchronizationContext);

                        offset += (uint)LibConsts.BUFF_NOTIFY_SIZE;
                        offset %= (uint)(LibConsts.BUFF_NOTIFY_SIZE * LibConsts.BUFF_NOTIFY_TIMES);

                        //Console.WriteLine("dwOffset = {0}, offset = {1}", this.rgdsbpn[notifyIdx].dwOffset, offset);
                    }
                    else if (notifyIdx == LibNatives.WAIT_FAILED)
                    {
                        int winErr = Marshal.GetLastWin32Error();

                        LibUtils.PrintLog("等待信号失败, LastWin32Error = {0}", winErr);
                        (state as SynchronizationContext).Send((o) =>
                        {
                            this.Stop();
                        }, null);
                        this.NotifyStatusChanged(DSLibPlayerStatus.Error, winErr);
                    }
                }

                LibUtils.PrintLog("跳出循环");
            }, SynchronizationContext.Current);

            return(LibErrors.SUCCESS);
        }
示例#2
0
        public int Start()
        {
            uint dsErr = this.dscb8.Start(LibNatives.DSCBSTART_LOOPING);

            if (dsErr != DSERR.DS_OK)
            {
                LibUtils.PrintLog("开始录音失败, DSERROR = {0}", dsErr);
                return(LibErrors.DS_ERROR);
            }

            this.isRunning = true;

            Task.Factory.StartNew((state) =>
            {
                while (this.isRunning)
                {
                    // 这里需要实时获取通知对象的指针, 因为这个指针的值每隔一段时间会改变。。。
                    IntPtr lpHandles = Marshal.UnsafeAddrOfPinnedArrayElement(this.notifyHwnd_close, 0);

                    // DSLibNatives.WaitForSingleObject(this.close_notifyHwnd[0], DSLibNatives.INFINITE);
                    switch (LibNatives.WaitForMultipleObjects(LibConsts.NotifyEvents, lpHandles, false, LibNatives.INFINITE))
                    {
                    case LibNatives.WAIT_OBJECT_0:
                        {
                            (state as SynchronizationContext).Send((o) =>
                            {
                                try
                                {
                                    byte[] audioData = null;
                                    if (this.RecordCapturedData(0, (uint)this.wfx.nAvgBytesPerSec, out audioData) == DSERR.DS_OK)
                                    {
                                        if (this.OnCaptured != null)
                                        {
                                            this.OnCaptured(audioData);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    LibUtils.PrintLog("保存音频流异常, Exception = {0}", ex);
                                }
                            }, null);

                            LibNatives.ResetEvent(this.notifyHwnd_close[0]);
                        }
                        break;

                    case LibNatives.WAIT_OBJECT_0 + 1:
                        {
                            // 录音结束
                            LibNatives.ResetEvent(this.notifyHwnd_close[1]);

                            this.isRunning = false;
                        }
                        break;

                    case LibNatives.WAIT_FAILED:
                        {
                            int error = Marshal.GetLastWin32Error();

                            // 失败, 句柄已经被销毁
                            LibUtils.PrintLog("WAIT_FAILED, LastWin32Error = {0}", error);

                            this.isRunning = false;

                            this.Stop();

                            if (this.OnError != null)
                            {
                                this.OnError(error);
                            }
                        }
                        break;
                    }
                }
            }, SynchronizationContext.Current);

            return(LibErrors.SUCCESS);
        }