简体   繁体   English

如何在C#中播放提取的波形文件字节数组?

[英]How to play extracted wave file byte array in C#?

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. 目前我已经设法分离了WAVE文件的左右声道,并将头部包含在byte []数组中。 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[]. 这部分是从右声道和光通道读取标题和数据,并将其保存到数组sLeft []和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: 添加了一个按钮来播放保存的wave文件:

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.dll System.dll中出现未处理的“System.InvalidOperationException”类型异常

Additional information: The wave header is corrupt. 附加信息:wave标头已损坏。

Any ideas? 有任何想法吗?

For "separate the left and right channel of a WAVE file" use AudioCompressionManager.SplitStereo method from http://alvas.net/alvas.audio.aspx 对于“分隔WAVE文件的左右声道”,请使用http://alvas.net/alvas.audio.aspx中的 AudioCompressionManager.SplitStereo方法。

Code below for your purpose 以下代码为您的目的

        IntPtr formatMono = AudioCompressionManager.CreateFormat(formatArray);
        IntPtr formatStereo = IntPtr.Zero;
        byte[] dataStereo = null;
        AudioCompressionManager.MergeStereo(formatMono, leftData, rightData, ref formatStereo, ref dataStereo);
        PlayerEx plex = new PlayerEx();
        plex.OpenPlayer(formatStereo);
        plex.AddData(dataStereo);
        plex.StartPlay();

I have a library up on codeplex for playing wave-form data. 我在codeplex上有一个用于播放波形数据的库。

http://WinMM.codeplex.com 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 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. 它有一些项目,因为一切都是通过插件基础设施加载的,但它确实展示了如何加载WAV文件。

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. 不幸的是,对于像这样的QA网站来说,你所拥有的问题有点过于“大”,但我很乐意离线与你讨论这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM