简体   繁体   English

在物理文件流中创建波文件有效,在内存流中创建失败,波头损坏

[英]create wavefile in physical filestream works, create in memorystream fails, waveheader corrupt

I have a buffer with Wav-samples, which I want to play.我有一个带有 Wav 样本的缓冲区,我想播放它。 Using a FileStream this works.使用FileStream这行得通。 Using a MemoryStream it fails with a System.InvalidOperationException 'The wave header is corrupt' on the soundPlayer.Play statement.使用MemoryStream失败,并在 soundPlayer.Play 语句中出现 System.InvalidOperationException 'The wave header is corrupt'。

This is the sourcethat works:这是有效的来源:

 Friend Sub New(buffer() As Byte, nBytes As Integer)
    'Dim wavStream As MemoryStream = New MemoryStream(CInt(nBytes + 44))
    Dim wavStream As FileStream = New FileStream("xxf.wav", FileMode.OpenOrCreate)
    waveFormat = New WaveFormat(44100, 16, 2)
    Wwriter = New WaveWriter(wavStream, waveFormat)
    Wwriter.Write(buffer, 0, nBytes)
    CType(Wwriter, IDisposable).Dispose()
    'soundPlayer = New System.Media.SoundPlayer(wavStream)
    soundPlayer = New System.Media.SoundPlayer("xxf.wav")
    soundPlayer.Play()
    soundPlayer.Dispose()
    soundPlayer = Nothing
    wavStream.Dispose()
    wavStream.Close()
End Sub

Changing the 2 commented lines, to use MemoryStream in stead of a FileStream, I get the 'The wave header is corrupt' error.更改 2 条注释行,以使用 MemoryStream 代替 FileStream,我收到“波形 header 已损坏”错误。 I rewrote the source in C#, with the same result.我重写了 C# 中的源代码,结果相同。 I watched the contents of the stream buffers in 3 places.我在 3 个地方查看了 stream 缓冲区的内容。 After writing the Wavheader, after writing the data, after disposing.写完Wavheader,写完数据,处理完。 The resulting differences are only in the Chunksize:由此产生的差异仅在 Chunksize 中:

after write of wavhdr:
 filestream   buffer(4) 12 127 26 0  (= 818970, the correct number of short samples)
 memorystream buffer(4) 36 0 0 0     (the number is there because I overwrite the same filedata)

after write of buffer:
 filestream   buffer(4) 12 127 26 0
 memorystream buffer(4) 36 0 0 0

after dispose: 
 identical  buffer(4) 12 127 26 0

I conclude that the stream is written correctly by the program, but not (yet?) written in memory by OS.我的结论是 stream 是由程序正确编写的,但不是(还没有?)由操作系统用 memory 编写。 But I am unable to find any Dispose or Close statements that I can add.但我找不到可以添加的任何 Dispose 或 Close 语句。 Wwriter itself has no dispose, but if I don't dispose of the file with IDisposable the FileStream version does not fail, but does not play anything either. Wwriter 本身没有处理,但如果我不使用 IDisposable 处理文件,则 FileStream 版本不会失败,但也不会播放任何内容。 WavStream may only be closed after playing, as the stream would be discarded. WavStream 只能在播放后关闭,因为 stream 将被丢弃。 Do you see what is my mistake?你明白我的错误是什么吗?

As is written in both comments, the problem is in reading a MemoryStream, after it was written.正如在两条评论中所写的,问题在于在写入 MemoryStream 之后读取它。 The absence of a close by the writing procedure and an open in the procedure that processes the data can only be overcome by changing the file's Position.只有通过更改文件的 Position 才能克服写入过程没有关闭和处理数据的过程中没有打开。 This needs always to be done, when a MemoryStream is used.当使用 MemoryStream 时,这总是需要完成的。 It will be written, it will be read, in between the pointer in the data needs to be reset to 0.它将被写入,它将被读取,在数据指针之间需要重置为 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM