This project has moved. For the latest updates, please go here.

NAudio and GetPosition() = 105840

Jun 4, 2013 at 11:21 AM
Edited Jun 4, 2013 at 11:24 AM
Hi,
Sorry for my english...
I've a problem with play any file *.ogg
I using this code:
       long dl;
       using (var vor = new NVorbis.NAudioSupport.VorbisWaveReader(@"D:\voice.ogg"))
       using (var wav = new NAudio.Wave.WaveOut())
       {
                 wav.Init(vor);
                 dl = vor.Length;
                 wav.Play();
                 while (wav.PlaybackState != NAudio.Wave.PlaybackState.Stopped)
                {
                      if (wav.GetPosition() > dl)
                      {
                            wav.Stop();
                       }
                      Thread.Sleep(100);
                 }
        }

if I create consoleapplication - I not have a problem with playing...
if I create wpfapplication, then I have a problem. this object wav.GetPosition() returned value 105840 and not play...

What do I very bad ? ....
Best regards...
Andrzej
Jun 4, 2013 at 12:01 PM
aboczko:

Looks like your code is running on the same thread as the UI. Try creating a separate thread to run the code above when running under WPF or WinForms.

Also, playback should stop automatically when the end of the stream is reached, so the position check should not be necessary.

Have fun!
Jun 4, 2013 at 10:08 PM
Edited Jun 5, 2013 at 10:12 AM
Thank you for your answer.
Also, playback should stop automatically when the end of the stream is reached, so the position check should not be necessary.
I know that, use another thread - to be able to control the playback: play / pause / stop
Looks like your code is running on the same thread as the UI. Try creating a separate thread to run the code above when running under WPF or WinForms.
Create a new thread does not work.
The process of playing the file to 105842 byte and stops. Proper operation of this is visible only when the console application. This is regardless of the file playback . ogg or . wave

Best regards.
Jun 6, 2013 at 3:51 PM
OK, try this:
  • Make vor and wav member variables on your UI class in the codebehind
  • In your "Play" button's click handler instantiate vor and wav, init wav, add a handler for wav.PlaybackStopped, then start playback. Do NOT use a "using" statement.
  • In the PlaybackStopped handler, put your cleanup code (wav.Dispose(); vor.Dispose();)
  • Add a "Stop" button and in its click handler call wav.Stop() if wav is currently playing.
I know this is different than the console code, but it should sidestep any message pumping issues that might be causing the behavior.

One side note: NVorbis is pretty fast, but may not be fast enough to decode in real-time on some slower machines.
Jun 7, 2013 at 7:51 AM
Thank you for your answer.

I do not know how or why, but it works. Earlier, as I tried it I had a problem with the initiation of objects Vor and Wav. I think I know why.

Thank you for the hint.

Best regards.