0

I want to share information how to create console C# application to play wav audio files using NAudio library. The only problem I currently have that in the end when audio file finished playing - it takes ~1sec to successfully terminate application. It could be because NAudio lib takes some time to free memory from audio data. Hope it will be helpful to someone.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NAudio.Wave;
using NAudio.CoreAudioApi;

namespace WaveTestNew
{
    class Program
    {
        static public WaveOut waveOut = new WaveOut();
        static public WaveFileReader reader = null;

        public static void printDevices()
        {
            Console.WriteLine("\nAvailable devices id:");

            for (int i = 0; i < WaveOut.DeviceCount; i++)
            {
                WaveOutCapabilities WOC = WaveOut.GetCapabilities(i);
                Console.WriteLine(i + " = " + WOC.ProductName);
            }
        }

        static void Main(string[] args)
        {

            if (args.Length < 2)
            {
                Console.WriteLine("Input parameters: <file_name> <device_id>");
                Console.WriteLine(@"Example:\n >>wavplay.exe c:\temp\test.wav 1");

                printDevices();
                Environment.Exit(1);
            }

            int iDeviceID = int.Parse(args[1]);
            if (iDeviceID >= WaveOut.DeviceCount)
            {
                Console.WriteLine("ERROR - unknown device_id: " + iDeviceID);
                printDevices();
                Environment.Exit(2);
            }

            String fileName = args[0];
            if (!fileName.ToLower().EndsWith(".wav"))
            {
                Console.WriteLine("ERROR: Not supported media file format");
                Environment.Exit(3);
            }

            waveOut.DeviceNumber = iDeviceID; // for default device
            reader = new WaveFileReader(fileName);
            waveOut.PlaybackStopped += waveOut_PlaybackStopped;
            waveOut.Init(reader);
            Console.WriteLine(DateTime.Now + ":Playback start");
            waveOut.Play();

            while (waveOut.PlaybackState != PlaybackState.Stopped)
            {
                Thread.Sleep(100);
            }
            Console.WriteLine(DateTime.Now + ":Playback stop");
            return;
        }

        private static void waveOut_PlaybackStopped(object sender, StoppedEventArgs e)
        {

            reader.Dispose();
            waveOut.Dispose();
        }
    }
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48
Volodymyr Prysiazhniuk
  • 1,897
  • 4
  • 22
  • 33
  • Try seeing if [this SO question](http://stackoverflow.com/questions/8655313/delay-approx-200-ms-in-streamed-audio-playback) helps. – Icemanind Apr 23 '14 at 23:56
  • I'm confused, is this a question or you want to "share code for others"? – pinkfloydx33 Apr 24 '14 at 00:38
  • It may clarify a bit to say that Volo posted this question answering it in the same post, then posted an answer, which was really more of a comment, it got flagged, and subsequently removed because of it. Ya, that doesnt really clarify does it. :) – crthompson Apr 25 '14 at 17:22
  • Sorry for confusing people here. I was looking for this solution by surfing many of websites and there was plenty of different way's of implementation, some functionality didn't work well when I try. My solution shows how it could work with console application. Also I want to give thanks to ppl who replied on my post and tried it on their computers. – Volodymyr Prysiazhniuk Apr 25 '14 at 20:50

1 Answers1

1

I have just tried your code sample and it returns right away, the issue you are having might be related to your hardware configuration.

If you can use another device, try it to see if it is related to the device you are currently using.

Alternatively you can try another library such as BASS.NET.

aybe
  • 15,516
  • 9
  • 57
  • 105