将 Memorystream 中的 MP3 转换为 WAV,以便使用 System.Media.Soundplayer 进行播放

发布于 2024-09-24 20:51:10 字数 2084 浏览 0 评论 0原文

我正在尝试使用 NAudio 库对一些声音效果进行一些内联​​转换,以便通过我的应用程序使用媒体声音播放器进行实际播放。 (我发现 NAudio 播放对于直接播放来说有点紧张,因此需要进行转换)

我真的不想将文件提取到磁盘上进行播放,而是想使用内存流将它们转换为内存中的 WAV。

查看 NAudio 源代码,它似乎不会将某些数据写入 Stream,直到将其释放为止,但这会导致 MemoryStream 无效。我应该重做还是我做错了?

使用以下代码我得到一个无效的标头。

如果我替换代码并将文件写入磁盘并处理然后重新加载到流,一切都很好(但它有点违背了目的)

Private Sub PlaySound(ByVal ms As Stream)
    Dim ss As System.Media.SoundPlayer
    ss = New System.Media.SoundPlayer
    ss.Stream = ms
    ss.Load()
    Try
        ss.PlaySync()
    Catch ex As InvalidOperationException
        Debug.Print(ex.ToString)
    End Try
End Sub

Private Sub ConvertToStream()
    Using reader As New Mp3FileReader("C:\KMSounds\sound12.mp3")
        Using convertedStream As WaveStream = WaveFormatConversionStream.CreatePcmStream(reader)
            'File.Delete("c:\test2.wav")
            'Using ms As New FileStream("c:\test2.wav", FileMode.Create)
            Using ms As New MemoryStream
                Using w As New WaveFileWriter(ms, convertedStream.WaveFormat)
                    Dim buffer() As Byte = CType(Array.CreateInstance(GetType(Byte), convertedStream.GetReadSize(4000)), Byte())
                    While True
                        Dim bytesRead As Integer = convertedStream.Read(buffer, 0, buffer.Length)
                        If (bytesRead = 0) Then
                            w.Flush()
                            Exit While
                        Else
                            w.WriteData(buffer, 0, bytesRead)
                        End If
                    End While
                    PlaySound(ms)
                End Using
            End Using
        End Using
    End Using
End Sub

System.InvalidOperationException:波头已损坏。 在 System.Media.SoundPlayer.ValidateSoundData(Byte[] 数据) 在 System.Media.SoundPlayer.LoadAndPlay(Int32 标志) 在 System.Media.SoundPlayer.PlaySync() 在 C:\Development\Spikes\NAudioConvert\ConvertAudio.vb 中的 NAudioConvert.ConvertAudio.Playfile(Stream ms):第 78 行

有关于此的任何提示吗?或者也许另一个我可以用来将一些非常小的 MP3 文件转换为 WAV 进行播放的库会很棒。

I'm trying to use the NAudio library to do some InLine conversion of some soundeffects for Playback with my application using the Media Soundplayer for the actual playback. (I find NAudio playback is a bit jittery for direct playback, hence the Conversion)

I really don't want to be extracting the files to disk to playback instead i'd like to convert them to WAV's in memory using a memoryStream.

Looking at the NAudio source it appears that it doesn't write some of the data to the Stream until it is disposed, but this therefore invalidates the MemoryStream. Should I rework that or am I doing it all wrong?

using the following code I get an invalidheader.

If I replace the code and write the file to disk and dispose then reload to a stream everything is fine (but it kinda defeats the purpose)

Private Sub PlaySound(ByVal ms As Stream)
    Dim ss As System.Media.SoundPlayer
    ss = New System.Media.SoundPlayer
    ss.Stream = ms
    ss.Load()
    Try
        ss.PlaySync()
    Catch ex As InvalidOperationException
        Debug.Print(ex.ToString)
    End Try
End Sub

Private Sub ConvertToStream()
    Using reader As New Mp3FileReader("C:\KMSounds\sound12.mp3")
        Using convertedStream As WaveStream = WaveFormatConversionStream.CreatePcmStream(reader)
            'File.Delete("c:\test2.wav")
            'Using ms As New FileStream("c:\test2.wav", FileMode.Create)
            Using ms As New MemoryStream
                Using w As New WaveFileWriter(ms, convertedStream.WaveFormat)
                    Dim buffer() As Byte = CType(Array.CreateInstance(GetType(Byte), convertedStream.GetReadSize(4000)), Byte())
                    While True
                        Dim bytesRead As Integer = convertedStream.Read(buffer, 0, buffer.Length)
                        If (bytesRead = 0) Then
                            w.Flush()
                            Exit While
                        Else
                            w.WriteData(buffer, 0, bytesRead)
                        End If
                    End While
                    PlaySound(ms)
                End Using
            End Using
        End Using
    End Using
End Sub

System.InvalidOperationException: The wave header is corrupt.
at System.Media.SoundPlayer.ValidateSoundData(Byte[] data)
at System.Media.SoundPlayer.LoadAndPlay(Int32 flags)
at System.Media.SoundPlayer.PlaySync()
at NAudioConvert.ConvertAudio.Playfile(Stream ms) in C:\Development\Spikes\NAudioConvert\ConvertAudio.vb:line 78

Any tips regarding this? or maybe another library that I could use to convert some very small MP3 files to WAV for playback would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-10-01 20:51:10

只有当您处理 WaveFileWriter 时,它才会返回到 WAV 标头并填充正确数量的数据字节(无需在每次调用 WriteData 时执行此操作)。但是,根据您的需要,您最好制作一个自定义 WaveFileWriter 来保持数据字节值最新。我正在考虑将其添加为 NAudio 未来版本的选项

it is only when you dispose the WaveFileWriter that it goes back to the WAV header and fills in the correct number of data bytes (saves doing it on every call to WriteData). However, for your needs, you'd be best off making a custom WaveFileWriter that does keep the data bytes value up to date. I'm considering adding this as an option for a future version of NAudio

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文