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

AudioFileReader position property always returns zero

Nov 12, 2011 at 3:32 PM

Hi,

I've just upgraded from 1.3 to the latest 1.4 changeset, and have been reworking my app to use the AudioFileReader (thanks for making this, BTW).  NAudio is playing beautifully except the AudioFileReader returns a "0" when I look at the position property (same thing when I look at the CurrentTime property).  The length property and Total Time properties appear correct. 

I'm using WASAPIOUT to output the media.

Config:  Windows 7 64 bit, NAudio target platform set for AnyCPU

 

Any thoughts on what I could be doing incorrectly?

 

Regards,

-John

Nov 12, 2011 at 7:12 PM

The problem seems to be threading related.  I'm running NAudio out of a console app (it will ultimately be a service) and I use a .NET timer to periodically check the value of the AudioFileReader Position attribute.  As a test, I read the Position attribute one second after I invoked the Play method (by putting the thread to sleep).  I was able to get a correct value for Position.  It appears that the .NET timers run the "Elapsed" event on a separate thread and is subsequently not providing the correct value for Position or CurrentTime. 

The demo apps appear to use the Windows Forms timers which execute their "Tick" method on the same thread as the GUI, so they are working correctly.  Does anyone have any thoughts on how to make this work? 

 

Regards,

-John

 

Dec 19, 2011 at 2:14 AM

I figured it out.  I needed to create an instance of the timer on the same thread that invokes the Play method on the WASAPIOUT provider.  My mistake (apparently) was creating the timer during the instantiation of the class making use of NAUDIO, and starting the timer just before I invoked the Play method.  Now, I create a new instance of the timer and start it just before I invoke Play,

Dec 19, 2011 at 8:57 AM

glad you got it working in the end

Mark