NAudio代码保持对输入文件的锁定

时间:2018-03-12 20:37:31

标签: c# .net naudio

我的代码会锁定输入文件,阻止我以后使用它:

public static void ToWave_WMF(string source, string destination)
{
    using (var reader = new MediaFoundationReader(source))
    using (var rateSampler = new MediaFoundationResampler(reader, new WaveFormat(DefaultEncoding.SampleRate, reader.WaveFormat.Channels)))
    using (var channelSampler = new MediaFoundationResampler(rateSampler, new WaveFormat(rateSampler.WaveFormat.SampleRate, DefaultEncoding.Channels)))
    {
        WaveFileWriter.CreateWaveFile(destination, channelSampler);
    }
}

public static string BuildWavFile(string userFileLocation)
{
    var sampleList = new List<ISampleProvider>();
    try
    {
        // Add input file
        var waveFile = AudioHelpers.ToWave_WMF(userFileLocation);
        sampleList.Add(new AudioFileReader(waveFile));

        WaveFileWriter.CreateWaveFile16(sirenWaveFile, new ConcatenatingSampleProvider(sampleList));
    }
    finally
    {
        foreach (var sample in sampleList)
        {
            ((AudioFileReader)sample).Dispose();
        }
    }

    return sirenWaveFile;
}

我使用资源错了吗?为什么要锁定?如果我在toWave_WMF()之后删除文件,则没有问题。如果我使用sampleList.Add(new AudioFileReader(userFileLocation));我也没有删除userFile的任何问题。

0 个答案:

没有答案