0
votes

I'm looking to use Naudio for a game I'm making.

I've coded a simple initialisation routine, which currently has:

                IWavePlayer wavePlayer1;
                AudioFileReader file1;
                string filename1 = "D:\\Programming\\SFX\\MUSIC\\IN_GAME_1.wav";

                wavePlayer1 = new WaveOut();
                file1 = new AudioFileReader(filename1);
                wavePlayer1.Init(file1);
                Thread thread1 = new Thread(new ThreadStart(wavePlayer1.Play));
                thread1.Start();

When I run the game, the wav file plays for maybe half a second, then it stops, and a second later it resumes.

If I place System.Threading.Thread.Sleep(5000) immediately after starting the thread, the delay has an additional 5 seconds. This seems to prove that 'thread1.Start();' isn't actually starting the sound in it's own thread at all.

Being a bit of a threading and Naudio noob can anyone tell me what I'm doing wrong here? My requirement for the game is to simply have the ability to play several sounds at once. Nothing major.

Any ideas?

Thanks in advance!

1
That thread runs for a couple of microseconds, just to call Play, then quits. Whatever the problem might be, it is not that thread. Maybe you ought to call this code more often, impossible to tell.Hans Passant

1 Answers

0
votes

Calling Play just starts playback. It doesn't block until playback has finished. So no need for the thread at all. Just call wavePlayer1.Play. You'll need to subscribe to PlaybackStopped so you can properly dispose your input file and playback device.