从数组对象 vb.net 播放 wav 文件时出现 wave header is corrupt 错误
The wave header is corrupt error when playing wav file from array object vb.net
我正在尝试构建一个应用程序,它说一个数字,例如 123 --> "one, two, three" 来自 wav 声音。
我可以使用以下命令多次播放相同的 wav 文件,没有任何问题:
My.Computer.Audio.Play(My.Resources.xxx, AudioPlayMode.WaitToComplete)
但是当我将对象放入这样的数组中时
wav_files() As Object =
{0,
My.Resources.xxx,
My.Resources.yyy,
My.Resources.zzz,
}
然后我尝试两次调用数组中的相同索引:
My.Computer.Audio.Play(wav_files(2), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wav_files(2), AudioPlayMode.WaitToComplete)
并不断出现错误:
The wave header is corrupt.
我需要将这些 wav 文件编入索引,以便我可以在我的转换函数 NUMBERS --> SOUND
中调用它们
我做的对吗?有没有其他想法,所以我的资源已经订购了索引文件。
编辑:
好的,这是有效的方法:
Dim wavs As New List(Of Byte())
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_1>))
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_2>))
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_3>))
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
或使用资源:
Using stream As System.IO.Stream = My.Resources.ResourceManager.GetStream(<STRING NAME OF RESOURCE WITHOUT EXTENSION>)
Dim buffer(stream.Length) As Byte
stream.Read(buffer, 0, stream.Length - 1)
wavs.Add(buffer)
End Using
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
确实如我所料,流位置需要重新设置。我刚刚尝试了这个并且它按预期工作:
Imports System.IO
Public Class Form1
Private sounds As UnmanagedMemoryStream() = {Nothing,
My.Resources.First,
My.Resources.Second,
My.Resources.Third}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.Audio.Play(sounds(1), AudioPlayMode.WaitToComplete)
sounds(1).Seek(0L, SeekOrigin.Begin)
My.Computer.Audio.Play(sounds(1), AudioPlayMode.WaitToComplete)
End Sub
End Class
鉴于您的意图是按数组索引播放声音,我可能会采用这种方式:
Imports System.IO
Public Class Form1
Private sounds As UnmanagedMemoryStream() = {Nothing,
My.Resources.First,
My.Resources.Second,
My.Resources.Third}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PlaySound(1)
PlaySound(1)
PlaySound(1)
PlaySound(2)
PlaySound(2)
PlaySound(2)
PlaySound(3)
PlaySound(3)
PlaySound(3)
End Sub
Private Sub PlaySound(index As Integer)
Dim sound = sounds(index)
My.Computer.Audio.Play(sound, AudioPlayMode.WaitToComplete)
sound.Seek(0L, SeekOrigin.Begin)
End Sub
End Class
这对我有用。
我正在尝试构建一个应用程序,它说一个数字,例如 123 --> "one, two, three" 来自 wav 声音。 我可以使用以下命令多次播放相同的 wav 文件,没有任何问题:
My.Computer.Audio.Play(My.Resources.xxx, AudioPlayMode.WaitToComplete)
但是当我将对象放入这样的数组中时
wav_files() As Object =
{0,
My.Resources.xxx,
My.Resources.yyy,
My.Resources.zzz,
}
然后我尝试两次调用数组中的相同索引:
My.Computer.Audio.Play(wav_files(2), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wav_files(2), AudioPlayMode.WaitToComplete)
并不断出现错误:
The wave header is corrupt.
我需要将这些 wav 文件编入索引,以便我可以在我的转换函数 NUMBERS --> SOUND
中调用它们我做的对吗?有没有其他想法,所以我的资源已经订购了索引文件。
编辑:
好的,这是有效的方法:
Dim wavs As New List(Of Byte())
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_1>))
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_2>))
wavs.Add(System.IO.File.ReadAllBytes(<PATH_TO_YOUR_SOUND_FILE_3>))
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
或使用资源:
Using stream As System.IO.Stream = My.Resources.ResourceManager.GetStream(<STRING NAME OF RESOURCE WITHOUT EXTENSION>)
Dim buffer(stream.Length) As Byte
stream.Read(buffer, 0, stream.Length - 1)
wavs.Add(buffer)
End Using
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
My.Computer.Audio.Play(wavs(0), AudioPlayMode.WaitToComplete)
确实如我所料,流位置需要重新设置。我刚刚尝试了这个并且它按预期工作:
Imports System.IO
Public Class Form1
Private sounds As UnmanagedMemoryStream() = {Nothing,
My.Resources.First,
My.Resources.Second,
My.Resources.Third}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.Audio.Play(sounds(1), AudioPlayMode.WaitToComplete)
sounds(1).Seek(0L, SeekOrigin.Begin)
My.Computer.Audio.Play(sounds(1), AudioPlayMode.WaitToComplete)
End Sub
End Class
鉴于您的意图是按数组索引播放声音,我可能会采用这种方式:
Imports System.IO
Public Class Form1
Private sounds As UnmanagedMemoryStream() = {Nothing,
My.Resources.First,
My.Resources.Second,
My.Resources.Third}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PlaySound(1)
PlaySound(1)
PlaySound(1)
PlaySound(2)
PlaySound(2)
PlaySound(2)
PlaySound(3)
PlaySound(3)
PlaySound(3)
End Sub
Private Sub PlaySound(index As Integer)
Dim sound = sounds(index)
My.Computer.Audio.Play(sound, AudioPlayMode.WaitToComplete)
sound.Seek(0L, SeekOrigin.Begin)
End Sub
End Class
这对我有用。