如何在 C# 中播放提取的波形文件字节数组?
目前,我已成功分离 WAVE 文件的左声道和右声道,并将标题包含在 byte[] 数组中。我的下一步是要播放两个频道。这怎么能做到呢?
这是一个代码片段:
byte[] song_left = new byte[fa.Length];
byte[] song_right = new byte[fa.Length];
int p = 0;
for (int c = 0; c < 43; c++)
{
song_left[p] = header[c];
p++;
}
int q = 0;
for (s = startByte; s < length; s = s + 3)
{
song_left[s] = sLeft[q];
q++;
s++;
song_left[s] = sLeft[q];
q++;
}
p = 0;
for (int c = 0; c < 43; c++)
{
song_right[p] = header[c];
p++;
}
这部分从右通道和光通道读取标题和数据并将其保存到数组 sLeft[] 和 sRight[] 中。这部分工作完美。
获得字节数组后,我执行了以下操作:
System.IO.File.WriteAllBytes("c:\\left.wav", song_left);
System.IO.File.WriteAllBytes("c:\\right.wav", song_right);
添加了一个按钮来播放保存的波形文件:
private void button2_Click(object sender, EventArgs e)
{
spWave = new SoundPlayer("c:\\left.wav");
spWave.Play();
}
一旦我点击播放按钮,就会出现此错误:
“System.InvalidOperationException”类型的未处理异常 发生在System.dll
附加信息:波形标头已损坏。
有什么想法吗?
At the moment i have managed to separate the left and right channel of a WAVE file and have included the header in a byte[] array. My next step is to be about to play both channels. How can this be done?
Here is a code snippet:
byte[] song_left = new byte[fa.Length];
byte[] song_right = new byte[fa.Length];
int p = 0;
for (int c = 0; c < 43; c++)
{
song_left[p] = header[c];
p++;
}
int q = 0;
for (s = startByte; s < length; s = s + 3)
{
song_left[s] = sLeft[q];
q++;
s++;
song_left[s] = sLeft[q];
q++;
}
p = 0;
for (int c = 0; c < 43; c++)
{
song_right[p] = header[c];
p++;
}
This part is reading the header and data from both the right and light channel and saving it to array sLeft[] and sRight[]. This part is working perfectly.
Once I obtained the byte arrays, I did the following:
System.IO.File.WriteAllBytes("c:\\left.wav", song_left);
System.IO.File.WriteAllBytes("c:\\right.wav", song_right);
Added a button to play the saved wave file:
private void button2_Click(object sender, EventArgs e)
{
spWave = new SoundPlayer("c:\\left.wav");
spWave.Play();
}
Once I hit the play button, this error appers:
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.dllAdditional information: The wave header is corrupt.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于“分离 WAVE 文件的左声道和右声道”,请使用 http://alvas 中的 AudioCompressionManager.SplitStereo 方法.net/alvas.audio.aspx
下面的代码适合您的目的
For "separate the left and right channel of a WAVE file" use AudioCompressionManager.SplitStereo method from http://alvas.net/alvas.audio.aspx
Code below for your purpose
我在 codeplex 上有一个用于播放波形数据的库。
http://WinMM.codeplex.com
我有一个项目,它在声音加载和播放方面做了很多工作上面的库:
http://files.otac0n.com/ShadySound.zip
(此链接可能需要一段时间才能开始工作,我刚刚创建了该子域。)
它包含一些项目,因为所有内容都是通过插件基础结构加载的,但它确实显示了如何加载 WAV 文件。
不幸的是,对于像这样的 QA 网站来说,您提出的问题有点太大了,但我很乐意与您离线讨论这个问题。
I have a library up on codeplex for playing wave-form data.
http://WinMM.codeplex.com
I have a project that does a lot with sound loading and playing based on the above library:
http://files.otac0n.com/ShadySound.zip
(This link may take a while to start working, i just created that subdomain.)
It has a few projects in it, because everything is loaded through a plug-in infrastructure, but it does show how to load a WAV file.
Unfortunately, the question you have is a little bit too "big" for a QA site like this, but I'd be happy to talk with you offline about this.