简体   繁体   English

C#:NAudio-空路径名不合法

[英]C#: NAudio - Empty path name is not legal

I've posted about this before detailing error messages, highlighting where the exception is being thrown, people seem to be ignoring what I write and just dive into the code making guesses. 在详细介绍错误消息,突出显示引发异常的位置之前,我已经发布了有关此内容的信息,人们似乎在忽略我编写的内容,而只是深入研究代码进行猜测。

So I'm going to post the code and while I admit I don't know EXACTLY what is wrong, I know where so I'd appreciate it if you could indulge my newbieness. 因此,我将发布代码,虽然我承认我完全不知道出什么问题,但我知道在哪里,所以如果您可以放纵我的新手,我将不胜感激。

WaveIn sourceStream = null;
    WaveFileWriter waveWriter = null;
    SaveFileDialog save = new SaveFileDialog();

 private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
    {
        if (waveWriter == null) return;
        //Adds bytes to the wave file, storing them in a buffer? 
        waveWriter.Write(e.Buffer, 0, e.BytesRecorded);
        waveWriter.Flush();
    }

    private void recordButton_Click(object sender, RoutedEventArgs e)
    {

        MainWindow mw = new MainWindow();
        //Get device number from MainWindow.DeviceButton().
        int devNum = mw.DeviceButton();

        if (recordButton.Content.ToString() == "RECORD")
        {
            recordButton.Content = "STOP";
            //Start recording audio. 
            sourceStream = new WaveIn();

            sourceStream.DeviceNumber = devNum;

            //1. Set the sample rate(?). 2. Get number of channels supported on the device.
            sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(devNum).Channels);


            waveWriter = new WaveFileWriter(save.FileName, sourceStream.WaveFormat);

            sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);
            sourceStream.StartRecording();
        }
        else if(recordButton.Content.ToString() == "STOP")
        {

            sourceStream.StopRecording();
            waveWriter.Dispose();
            save.Filter = "Wave Files (*.wav)|*.wav;";
                if (save.ShowDialog() != true) return;



            recordButton.Content = "RECORD";



        }
    }

The error is on this line: 错误在此行上:

waveWriter = new WaveFileWriter(save.FileName, sourceStream.WaveFormat);

It is a runtime error, triggering a System.ArgumentException. 这是一个运行时错误,触发System.ArgumentException。 Very general, the only useful thing I get is that 'Empty path name is not legal' message. 非常笼统,我得到的唯一有用的信息是“空路径名不合法”消息。

Now, I'm following along with some example code in order to familiarize myself with NAudio and they don't ever actually set save.FileName(). 现在,我将跟随一些示例代码,以使自己熟悉NAudio,而它们实际上并没有设置save.FileName()。 So naturally I didn't, honestly I don't think it's necessary or the problem, but thought it worth mentioning just in case. 因此,自然而然地,我没有必要,也没有问题,但是我认为值得一提,以防万一。

Personally, and I don't see this in the sample code either, I don't see the connection between the save dialogue and actually saving my audio stream. 就个人而言,我也没有在示例代码中看到这一点,也没有看到保存对话框与实际保存音频流之间的联系。 Perhaps I'm missing this component? 也许我缺少此组件?

I see the call to: 我看到了以下呼叫:

SaveFileDialog save = new SaveFileDialog();

However, I do not see where you call 但是,我看不到你在哪里打电话

save.ShowDialog()

before you reference the file name. 在引用文件名之前。 You will need to make that call in order to select the path/filename. 您需要进行该呼叫才能选择路径/文件名。 You should then check to see if the FileName is empty before calling a method that references it. 然后,应在调用引用该文件的方法之前检查FileName是否为空。 For example, 例如,

if (!string.IsNullOrEmpty(save.FileName)) 
{
    <Code Goes Here>
}

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

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