我有一点问题:在 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
如何编写结果流以确保以后可以再次读取它?
任何想法将不胜感激。