在WPF

时间:2015-06-19 13:13:52

标签: c# wpf playback naudio memorystream

我尝试连接不同持续时间但相同采样率的一些数据。 我用MemoryStream.CopyTo(memoryStream)方法连接数据。

有没有办法从内存流中播放一个没有wav头的数据?

如果没有,是否有任何方法可以在添加流之后附加wav标头以启动?

(我想避免在磁盘中生成文件..)

2 个答案:

答案 0 :(得分:1)

要使用NAudio执行此操作,只需使用RawSourceWaveStream并将包含原始连接音频的MemoryStream传递给其构造函数。您还需要明确指定WaveFormat是。

memoryStream = ... // construct your audio
memoryStream.Position = 0; // rewind to beginning
var rs = new RawSourceWaveStream(memoryStream, new WaveFormat(sampleRate, 16, 1));

答案 1 :(得分:0)

您可以在流中编写标题,然后使用SoundPlayer播放WAV。

    //your wav streams
    MemoryStream wavNoHeader1=new MemoryStream();
    MemoryStream wavNoHeader2 = new MemoryStream();
    //result WAV stream
    MemoryStream wav=new MemoryStream();
    //create & write header
    ushort numchannels = 1;
    ushort samplelength = 1; // in bytes
    uint samplerate = 22050;
    int wavsize = (int) ((wavNoHeader1.Length + wavNoHeader2.Length)/(numchannels*samplelength));
    BinaryWriter wr = new BinaryWriter(wav);
    wr.Write(Encoding.ASCII.GetBytes("RIFF"));
    wr.Write(36 + wavsize);
    wr.Write(Encoding.ASCII.GetBytes("WAVEfmt "));
    wr.Write(16);
    wr.Write((ushort)1);
    wr.Write(numchannels);
    wr.Write(samplerate);
    wr.Write(samplerate * samplelength * numchannels);
    wr.Write(samplelength * numchannels);
    wr.Write((ushort)(8 * samplelength));
    wr.Write(Encoding.ASCII.GetBytes("data"));
    wr.Write(numsamples * samplelength);
    //append data from raw streams
    wavNoHeader1.CopyTo(wav);
    wavNoHeader2.CopyTo(wav);
    //play
    wav.Position = 0;
    SoundPlayer sp=new SoundPlayer(wav);
    sp.Play();