private Device PlayDev;
private IntPtr intptr;FileStream  file= File.Open(@"C:\xp.wav", FileMode.Open, FileAccess.Read); 
Int32 end = (Int32)file.Length;
byte[] b = new byte[end];
file.Read(b, 0, b.Length);
getvoids(b);
public void getvoids(byte[] bytRecv){
if (!CreatePlayDevice())
{
throw new Exception();
}
BufferDescription buffDiscript = new BufferDescription();
WaveFormat mWavFormat = SetWaveFormat();
buffDiscript.Format = mWavFormat;
buffDiscript.BufferBytes = 4410;
buffDiscript.ControlPan = true;
buffDiscript.ControlFrequency = true;
buffDiscript.ControlVolume = true;
buffDiscript.GlobalFocus = true;
SecondaryBuffer sec = new SecondaryBuffer(buffDiscript, PlayDev);
sec.Write(0, bytRecv, LockFlag.FromWriteCursor);
sec.Play(0, BufferPlayFlags.Default);
}
private WaveFormat SetWaveFormat()
{
WaveFormat format = new WaveFormat();
format.FormatTag = WaveFormatTag.Pcm;//设置音频类型
format.SamplesPerSecond = 11025;//采样率(单位:赫兹)典型值:11025、22050、44100Hz
format.BitsPerSample = 16;//采样位数
format.Channels = 1;//声道
format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));//单位采样点的字节数
format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
return format;
}private bool CreatePlayDevice()
{
DevicesCollection dc = new DevicesCollection();
Guid g;
if (dc.Count > 0)
{
g = dc[0].DriverGuid;
}
else
{ return false; }
PlayDev = new Device(g);
PlayDev.SetCooperativeLevel(this.Handle, CooperativeLevel.Normal);
return true;
}
当执行到“sec.Write(0, bytRecv, LockFlag.FromWriteCursor);”时就提示“值不在预期的范围内。”

解决方案 »

  1.   

    Microsoft.DirectX.DirectSound.Device dev =
                    new Microsoft.DirectX.DirectSound.Device();
               dev.SetCooperativeLevel(this,              Microsoft.DirectX.DirectSound.CooperativeLevel.Normal);
               try
                {
                    Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
                    new Microsoft.DirectX.DirectSound.SecondaryBuffer(
                      "../A.wav", dev);
                    snd.Play(0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default);
                }
                catch (Exception ex)
                {
                 }
    http://blog.csdn.net/21aspnet/archive/2007/03/24/1539655.aspx
      

  2.   

    wuyq11  
    我要的是先要把WAV文件转为byte[],目的是用于网络上的传输
      

  3.   

    wuyq11 
    我要的是把WAV文件转为byte[]后再播放,目的是因为是为音频网络传输的
      

  4.   

    去google一下什么叫流媒体...WAV是不可以这样播放的...
      

  5.   

    SecondaryBuffer可以直接接收文件名或者Stream作为参数的。你代码的问题是
    buffDiscript.BufferBytes = 4410;
    而你实际的buffer大小是bytRecv.Length,所以会报错