我有一个问题:在VS2010 win 7编译为x86的winForm应用程序中,我尝试执行Alvas.Audio似乎有效的操作。有关参考,请参见 (c# ex: http://alvas.net/alvas.audio,tips.aspx#tip94)。
Dim data() As Byte = wr.ReadData(second * i, second)
结果给了我data.length()=0
.我没有任何例外,我可以从中读取格式,无论我使用什么阅读器,我都遇到了这个问题。
编辑:经过一些测试,似乎我在第一步中创建的未压缩文件(PCM格式,扩展名.wav)无法被Alvas.audio库识别第二步。我必须错过有关音频文件标记或类似内容的内容。
以下是可能是源代码的代码(基本上这是第 1 步):
Dim functOut As String = String.Empty
Dim wr As Alvas.Audio.IAudioReader = Nothing
Dim fs As IO.FileStream = Nothing
Dim i As Integer = 0
Dim tmpData() As Byte = Nothing
Dim dataPCM() As Byte = Nothing
Dim newFormat As IntPtr = IntPtr.Zero
Try
Select Case IO.Path.GetExtension(filename).ToLower()
Case ".wav"
wr = New Alvas.Audio.WaveReader(IO.File.OpenRead(filename))
filename = IO.Path.GetTempPath & IO.Path.GetFileNameWithoutExtension(filename) & "2" & IO.Path.GetExtension(filename)
Case ".mp3"
wr = New Alvas.Audio.Mp3Reader(IO.File.OpenRead(filename))
Case Else : wr = New Alvas.Audio.DsReader(filename)
End Select
functOut = IO.Path.ChangeExtension(filename, ".wav")
Dim format As IntPtr = wr.ReadFormat()
Dim formatDetail As Alvas.Audio.WaveFormat = Alvas.Audio.AudioCompressionManager.GetWaveFormat(format)
If formatDetail.wFormatTag = Alvas.Audio.AudioCompressionManager.MpegLayer3FormatTag Then
Alvas.Audio.AudioCompressionManager.Mp3ToWav(filename, functOut)
Return True
Else
IO.File.Create(functOut).Close()
While True
tmpData = wr.ReadData(SECONDS * i, SECONDS)
If tmpData Is Nothing Or tmpData.Length = 0 Then Exit While
If formatDetail.wBitsPerSample < 16 Then
Alvas.Audio.AudioCompressionManager.ToPcm16Bit(format, tmpData, newFormat, dataPCM)
Else
newFormat = format
dataPCM = tmpData
End If
formatDetail = Alvas.Audio.AudioCompressionManager.GetWaveFormat(newFormat)
fs = IO.File.Open(filename, IO.FileMode.Append, IO.FileAccess.Write)
Using ww As New Alvas.Audio.WaveWriter(fs, Alvas.Audio.AudioCompressionManager.FormatBytes(newFormat))
ww.Write(dataPCM, 0, dataPCM.Length())
End Using
i += 1
tmpData = Nothing
End While
Return True
End If
wr.Close()
Catch ex As Exception
filename = String.Empty
Throw ex
Finally
fs.Close()
wr.Close()
filename = functOut
GC.Collect()
End Try
如何编写结果流以确保以后可以再次读取它?
任何想法将不胜感激。
我找到了解决方案。
PCM 文件是无标头的。因此,当保存时,即使使用waveWriter和行Alvas.Audio.AudioCompressionManager.FormatBytes(newFormat)
也被忽略。
可以做两件事:
-
找到一个识别无头文件并允许从中读取的阅读器。
-
重构代码以解码为 PCM 文件并将其编码为新格式(例如 .mp3),而无需将结果文件写入文件系统(更好)