Search code examples
c#visual-studio-2013naudio

Error 13 in Visual Studio 2013 Express: output name


So I am trying to build a media player and I am following a tutorial on YouTube.
I followed the tutorial and entered the code 1 for 1 and all of the instances of the name 'output' gave an Error 13. I have added the NAudio 1.6.3 dll as well.Maybe I got the wrong version?

Here is the link to the YouTube video... C# Audio Tutorial 1 - Wave File with NAudio

And here is my code...

    using System;
using System.Windows.Forms;

namespace Naudio_Tutorial
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private NAudio.Wave.WaveFileReader wave = null;

        private NAudio.Wave.DirectSoundOut wave = null;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "Wave File (*.wav)|*.wav;";
            if (open.ShowDialog() != DialogResult.OK) return;


            DisposeWave();

            wave = new NAudio.Wave.WaveFileReader(open.FileName);
            Output = new NAudio.Wave.DirectSoundOut();
            Output.Init(new NAudio.Wave.WaveChannel32(wave));
            Output.Play();

            pauseButton.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Pause();
                else if (output.PlaybackState == NAudio.Wave.PlaybackState.Paused) output.Play();

            }
        }

        private void DisposeWave()
        {
            if (output != null)
            {
                if (output.PlaybackState == NAudio.Wave.PlaybackState.Playing) output.Stop();
                output.Dispose();
                output = null;
            }
            if (wave != null)
            {
                wave.Dispose();
                wave = null;
            }
        }
        private void Form1_Closing(object sender, FormClosingEventArgs e)
        {
            DisposeWave();
        }
    }
}

Solution

  • First thing I notice is that you are declaring two different objects with the same name. In my experience, the application shouldn't even compile because this would be a conflict.

    This line:

    private NAudio.Wave.DirectSoundOut wave = null;
    

    Should be changed to:

    private NAudio.Wave.DirectSoundOut output = null;
    

    The compiler will complain because you are attempting to access an object that has not been declared. The following will cause an exception such as the one you have described:

    Output = new NAudio.Wave.DirectSoundOut();
    Output.Init(new NAudio.Wave.WaveChannel32(wave));  
    Output.Play();
    

    Also, just a tip. Error 13 isn't descriptive enough. Including the entire error message and stacktrace will help others help you.

    Error 13 - ERROR_INVALID_DATA