This project has moved and is read-only. For the latest updates, please go here.

How to Playback audio on a separate Thread

Apr 23, 2014 at 10:29 PM
Edited Apr 23, 2014 at 10:32 PM
Hi

I have NAudio playing audio from my main Form1, meaning all audio is playing from the primary Thread, but is there a way to Playback audio on a separate Thread (not using backgroundworker) and be able to control Pause, Stop, etc?,

Here is the only facts I know:
  • This thread will have to remain open clearly for the time of the playback, otherwise the audio will be terminated.
Please help.
Thanks.
Apr 23, 2014 at 10:38 PM
yes, use WaveOutEvent
Apr 24, 2014 at 12:46 AM
Edited Apr 24, 2014 at 12:52 AM
I just changed it to WaveOutEvent, not sure if I'm doing this right though, the Thread starts when I click on buttonPlay, the Thread ends immediately right after it starts, unless I use Sleep for a few seconds, but this is not a problem, what I'm concern is that the audio keeps on playing even when the Thread is finished, is that accurate?, I mean, isn't the audio suppose to stop when the thread is closed?
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;

namespace NAudioTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        public WaveOutEvent waveOutDevice;
        AudioFileReader audioFileReader;

        Thread t;
        
        private void buttonPlay_Click(object sender, EventArgs e)
        {
            if (waveOutDevice == null)
            {
                    t = new Thread(startThread);
                    t.Start();              
            }            
        }


        private void startThread()
        {
            waveOutDevice = new WaveOutEvent();
            audioFileReader = new AudioFileReader(@"C:\Audio\Give Your Heart A Break.mp3");
            waveOutDevice.Init(audioFileReader);
            waveOutDevice.Play();
            Thread.Sleep(3000);
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            t.Abort();  
            if (waveOutDevice != null)
            {
                waveOutDevice.Stop();
            }
            if (waveOutDevice != null)
            {
                waveOutDevice.Dispose();
                waveOutDevice = null;
            }
        }

        private void buttonCheckThread_Click(object sender, EventArgs e)
        {
            if (t.IsAlive)
            {
                MessageBox.Show("Is Alive");
            }
            else if (t == null)
            {
                MessageBox.Show("Is Null");
            }
            else
            {
                MessageBox.Show("Other");
            }
        }

    }

}
Apr 24, 2014 at 9:34 AM
You don't need to create the thread yourself, WaveOutEvent will do it for you when you call play.
Marked as answer by aj152 on 4/24/2014 at 9:18 AM
Apr 24, 2014 at 2:39 PM
Oh I see. Thank you.

And do you know if I can use WaveOutEvent with DirectSound?, or WaveOutEvent is already like the only driver output plugin available for this scenario?
Apr 24, 2014 at 2:48 PM
No, DirectSound works the same - it creates a thread. So you can use it instead.
Apr 24, 2014 at 2:58 PM
Got it, thank you. Cheers!