wav 文件连接异常与 naudio 库

wav file concatenation exception with naudio library

我使用了我在这个论坛上找到的以下方法。 在我的 WPF 用户界面中,我有一个按钮调用此方法以连接一些声音文件并播放新文件。 第一次点击后,连接文件生成,我等待播放结束 file.Then,我想创建一个新的连接文件,但是 - 如果我再次点击,我在这行代码中得到一个异常:

// first time in create new Writer
waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);

“mscorlib.dll 中发生类型 'System.IO.IOException' 的异常,但未在用户代码中处理

附加信息:该进程无法访问文件 'C:\Users\alon\Documents\Visual Studio 2013\Projects\MyMahappsExtended\MyMahappsExtended\bin\Debug\Sounds\mySendFile.wav',因为它正被另一个进程使用。”

我不知道问题出在哪里。

public static void Concatenate(string outputFile, IEnumerable<string> sourceFiles)
    {
        byte[] buffer = new byte[8820];
        WaveFileWriter waveFileWriter = null;

        try
        {
            foreach (string sourceFile in sourceFiles)
            {
                using (WaveFileReader reader = new WaveFileReader(sourceFile))
                {
                    if (waveFileWriter == null)
                    {
                        // first time in create new Writer
                        waveFileWriter = new WaveFileWriter(outputFile, reader.WaveFormat);
                    }
                    else
                    {
                        if (!reader.WaveFormat.Equals(waveFileWriter.WaveFormat))
                        {
                            throw new InvalidOperationException("Can't concatenate WAV Files that don't share the same format");
                        }
                    }
                    int read;
                    while ((read = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        waveFileWriter.WriteData(buffer, 0, read);
                    }
                }
            }
        }
        finally
        {
            if (waveFileWriter != null)
            {
                waveFileWriter.Dispose();
            }
        }
    }

更新: 此代码调用上述代码:

private async void mySendButton_Click(object sender, RoutedEventArgs e)
    {
        myRing.IsActive = true;
        List<string> myList = new List<string>();
        //Here I want to add to the list a Start Delimiter
        myList.Add("Sounds\silence.wav");
        myList.Add("Sounds\10000_0.2s.wav");

        //string bits = GetBits(myTextBox.Text);
        string bits = GetBits2(new TextRange(myTextBox.Document.ContentStart, myTextBox.Document.ContentEnd).Text);
        foreach (char c in bits)
        {
            if (c == '0')
            {
                myList.Add("Sounds\13000_0.015s.wav");
            }
            else
            {
                myList.Add("Sounds\13100_0.015s.wav");
            }
        }
        //Here I want to add to the list a Finish Delimiter

        myList.Add("Sounds\11000_0.2s.wav");
        myList.Add("Sounds\silence.wav");

        Concatenate("Sounds\mySendFile.wav", myList);

        MediaPlayer mplayer = new MediaPlayer();
        mplayer.Open(new Uri("Sounds\mySendFile.wav", UriKind.Relative));
        mplayer.Play();
        await Task.Delay((int)(1000*(bits.Length*BitTime+4*StartDelimiterTime+4*EndDelimiterTime)));
        myRing.IsActive = false;

    }

在 await 语句之后我必须关闭 MediaPlayer class:

mplayer.close();

感谢:@CodeCaster:https://whosebug.com/users/266143/codecaster