wav file concatenation exception with naudio library

803 views Asked by At

I use the following method which I found here at this forum. In my WPF user interface I have a button the calls this method in order to concatenate some sound files and plays the new file. After the first click the concatenated file is generated and I wait for the end of the played file.Then, I want to create a new concatenated file, But - if I click again I get an exception in this line of code:

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

"An exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code

Additional information: The process cannot access the file 'C:\Users\alon\Documents\Visual Studio 2013\Projects\MyMahappsExtended\MyMahappsExtended\bin\Debug\Sounds\mySendFile.wav' because it is being used by another process."

I can't get where is the problem.

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();
            }
        }
    }

Update: This code calls the aboved one:

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;

    }
1

There are 1 answers

0
axcelenator On BEST ANSWER

after the await statement I have to close the MediaPlayer class:

mplayer.close();

Credits to : @CodeCaster: https://stackoverflow.com/users/266143/codecaster