日期:2014-05-20  浏览次数:20987 次

1000分求个录音&保存&回放的例程(帮顶得这贴的100分)
RT,如果直接给出Mp3格式的东西,再加500分。

------解决方案--------------------
我新手,没做过
友情支持
帮顶

------解决方案--------------------
路过,帮顶
------解决方案--------------------
友情帮顶
------解决方案--------------------
http://www.cnblogs.com/KeithDan/archive/2006/12/19/596833.html
------解决方案--------------------
using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace WaveLib
{
internal class WaveInHelper
{
public static void Try(int err)
{
if (err != WaveNative.MMSYSERR_NOERROR)
throw new Exception(err.ToString());
}
}

public delegate void BufferDoneEventHandler(IntPtr data, int size);

internal class WaveInBuffer : IDisposable
{
public WaveInBuffer NextBuffer;

private AutoResetEvent m_RecordEvent = new AutoResetEvent(false);
private IntPtr m_WaveIn;

private WaveNative.WaveHdr m_Header;
private byte[] m_HeaderData;
private GCHandle m_HeaderHandle;
private GCHandle m_HeaderDataHandle;

private bool m_Recording;

internal static void WaveInProc(IntPtr hdrvr, int uMsg, int dwUser, ref WaveNative.WaveHdr wavhdr, int dwParam2)
{
if (uMsg == WaveNative.MM_WIM_DATA)
{
try
{
GCHandle h = (GCHandle)wavhdr.dwUser;
WaveInBuffer buf = (WaveInBuffer)h.Target;
buf.OnCompleted();
}
catch
{
}
}
}

public WaveInBuffer(IntPtr waveInHandle, int size)
{
m_WaveIn = waveInHandle;

m_HeaderHandle = GCHandle.Alloc(m_Header, GCHandleType.Pinned);
m_Header.dwUser = (IntPtr)GCHandle.Alloc(this);
m_HeaderData = new byte[size];
m_HeaderDataHandle = GCHandle.Alloc(m_HeaderData, GCHandleType.Pinned);
m_Header.lpData = m_HeaderDataHandle.AddrOfPinnedObject();
m_Header.dwBufferLength = size;
WaveInHelper.Try(WaveNative.waveInPrepareHeader(m_WaveIn, ref m_Header, Marshal.SizeOf(m_Header)));
}
~WaveInBuffer()
{
Dispose();
}

public void Dispose()
{
if (m_Header.lpData != IntPtr.Zero)
{
WaveNative.waveInUnprepareHeader(m_WaveIn, ref m_Header, Marshal.SizeOf(m_Header));
m_HeaderHandle.Free();
m_Header.lpData = IntPtr.Zero;
}
m_RecordEvent.Close();
if (m_HeaderDataHandle.IsAllocated)
m_HeaderDataHandle.Free();
GC.SuppressFinalize(this);
}

public int Size
{
get { return m_Header.dwBufferLength; }
}

public IntPtr Data
{
get { return m_Header.lpData; }
}

public bool Record()
{
lock(this)
{
m_RecordEvent.Reset();
m_Recording = WaveNative.waveInAddBuffer(m_WaveIn, ref m_Header, Marshal.SizeOf(m_Header)) == WaveNative.MMSYSERR_NOERROR;
return m_Recording;
}
}

public void WaitFor()
{
if (m_Recording)
m_Recording = m_RecordEvent.WaitOne();
else
Thread.Sleep(0);
}

private void OnCompleted()
{
m_RecordEvent.Set();
m_Recording = false;
}
}

public class WaveInRecorder : IDisposable
{
private IntPtr m_WaveIn;