/// <summary> /// 如果播放正在进行,则停止播放声音。 /// </summary> public void Stop() { m_Paused = false; m_IsPlaying = false; if (m_DeviceID != 0) { MCI_GENERIC_PARMS gp = new MCI_GENERIC_PARMS(); gp.dwCallback = IntPtr.Zero; IntPtr gpPointer = Marshal.AllocHGlobal(Marshal.SizeOf(gp)); try { Marshal.StructureToPtr(gp, gpPointer, false); UInt32 errorCode = mciSendCommand(m_DeviceID, (UInt32)MCI_Command.MCI_STOP, (UInt32)MCI_OpenFlags.MCI_WAIT, gpPointer); CheckError(errorCode); m_IsPlaying = false; } finally { Marshal.FreeHGlobal(gpPointer); } Close(); } }
/// <summary> /// 暂停音频播放 /// </summary> public void Pause() { if (m_DeviceID != 0) { MCI_GENERIC_PARMS gp = new MCI_GENERIC_PARMS(); gp.dwCallback = IntPtr.Zero; IntPtr gpPointer = Marshal.AllocHGlobal(Marshal.SizeOf(gp)); try { Marshal.StructureToPtr(gp, gpPointer, false); UInt32 errorCode = mciSendCommand(m_DeviceID, (UInt32)MCI_Command.MCI_PAUSE, (UInt32)MCI_OpenFlags.MCI_WAIT, gpPointer); CheckError(errorCode); m_Paused = true; } finally { Marshal.FreeHGlobal(gpPointer); } } else { throw new InvalidOperationException("Sound device has already closed."); } }
/// <summary> /// 播放快进(每次快进 FramesStep) /// </summary> public void Forward() { int err = 0; StringBuilder buf = new StringBuilder(1000); TimeFormat tf = timeFormat; if (deviceID > 0) { if ((devCaps & DevCaps.CanShowVideo) == DevCaps.CanShowVideo) { if (GetLength() - GetPosition() <= framesStep) { MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); Pause(); err = mciSendCommandA(deviceID, MCI_SEEK, MCI_SEEK_TO_END | MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } Play(); } else { MCI_DGV_STEP_PARMS step = new MCI_DGV_STEP_PARMS(); Pause(); if (timeFormat != TimeFormat.Frames) { timeFormat = TimeFormat.Frames; MciSet(0, MCI_FORMAT_FRAMES, MCI_SET_TIME_FORMAT); } step.dwFrames = framesStep; err = mciSendCommandA(deviceID, MCI_STEP, MCI_WAIT | MCI_DGV_STEP_FRAMES, ref step); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } if (tf != TimeFormat.Frames) { timeFormat = tf; MciSet(0, (int)tf, MCI_SET_TIME_FORMAT); } Play(); } } else { if (timeFormat != TimeFormat.MilliSeconds) { timeFormat = TimeFormat.MilliSeconds; MciSet(0, MCI_FORMAT_MILLISECONDS, MCI_SET_TIME_FORMAT); } SeekToAny(GetPosition() + framesStep); if (tf != TimeFormat.MilliSeconds) { timeFormat = tf; MciSet(0, (int)tf, MCI_SET_TIME_FORMAT); } } } }
/// <summary> /// 关闭当前播放文件。 /// </summary> public void Close() { int err; StringBuilder buf = new StringBuilder(1000); if (deviceID > 0) { Stop(); MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); err = mciSendCommandA(deviceID, MCI_CLOSE, MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } deviceID = 0; devCaps = DevCaps.None; size = Size.Empty; if ((int)timer.Tag == 1) { timer.Tag = 0; timer.Stop(); } } }
/// <summary> /// 停止当前播放文件。 /// </summary> public void Stop() { int err; StringBuilder buf = new StringBuilder(1000); if (deviceID > 0 && PlayState == PlayState.Play || PlayState == PlayState.Record) { Previous(); MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); err = mciSendCommandA(deviceID, MCI_STOP, MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } else { if ((int)timer.Tag == 1) { timer.Tag = 0; timer.Stop(); } } } }
/// <summary> /// 继续播放。 /// </summary> public void Resume() { int err; StringBuilder buf = new StringBuilder(1000); if (deviceID > 0 && PlayState == PlayState.Pause) { MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); err = mciSendCommandA(deviceID, MCI_RESUME, MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } else { if ((int)timer.Tag == 0) { timer.Tag = 1; timer.Start(); } } } }
/// <summary> /// 使当前播放文件跳到上一首(TMSF)或开始位置。 /// </summary> public void Previous() { int err; StringBuilder buf = new StringBuilder(1000); if (deviceID > 0) { if (timeFormat == TimeFormat.TMSF) { int pos = CurrentTrack; Pause(); //如果不是第一首,跳到上一首,否则重新开始第一首。 if (pos != 1) { StartPos = pos - 1; } else StartPos = pos; Play(); } else { MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); Pause(); err = mciSendCommandA(deviceID, MCI_SEEK, MCI_SEEK_TO_START | MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } Play(); } } }
/// <summary> /// 使当前播放文件跳到下一首(TMSF)或最后。 /// </summary> public void Next() { int err; StringBuilder buf = new StringBuilder(1000); if (deviceID > 0) { if (timeFormat == TimeFormat.TMSF) { Pause(); int pos = CurrentTrack; int tracks = Tracks; //如果是最后一首,重新开始最后一首,否则下一首。 if (pos == tracks) { StartPos = tracks; } else { StartPos = pos + 1; } Play(); } else { MCI_GENERIC_PARMS gen = new MCI_GENERIC_PARMS(); Pause(); err = mciSendCommandA(deviceID, MCI_SEEK, MCI_SEEK_TO_END | MCI_WAIT, ref gen); if (err != 0) { mciGetErrorStringA(err, buf, 1000); //throw new ApplicationException(buf.ToString()); } Play(); } } }
private static extern int mciSendCommandGeneric(int wDeviceID, int uMessage, int dwParam1, ref MCI_GENERIC_PARMS dwParam2);
private static extern int mciSendCommandA( Int32 wDeviceID, UInt32 uMessage, Int32 dwParam, ref MCI_GENERIC_PARMS Any);