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

naudio seekbar

Jan 25, 2014 at 6:48 AM
Hi, I just starting to use naudio. I currently creating a seekbar that can seek to any position of the song I want using C#. It is something like MP3 Player function. I have successfully load the song Totaltime into trackbar but fail to seek the song and the seekbar also fail to run as the song start playing. Below is my current code for the seekbar:

private void TrackPosition_ValueChanged(object sender, System.EventArgs e)
        TrackPosition.Minimum = 0;

        TrackPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;

        TrackPosition.Value = (int)mainOutputStream.CurrentTime.TotalSeconds;

        mainOutputStream.Position = mainOutputStream.Position + TrackPosition.Value;

The TrackPosition is the seekbar I mentioned and I have loaded the song Totaltime into the seekbar but I fail to seek the song and make the seekbar run when the song is playing.
Do I left out something? Or anything wrong with the code? Sorry if I done anything wrong, I new to C#. Thanks for the help.
Jan 25, 2014 at 8:55 AM
Edited Jan 25, 2014 at 11:15 AM
Try this:
TrackPosition.Minimum = 0;

TrackPosition.Maximum = mainOutputStream.TotalTime.TotalSeconds;

TrackPosition.Value = mainOutputStream.CurrentTime.TotalSeconds;

mainOutputStream.Position = TrackPosition.Value;
Jan 25, 2014 at 10:14 AM
Hi K24A3, thx for replying. I have tried your method. The song start over again when I click the seekbar and the seekbar still cant run and cant seek the song. If you have other method you can share it out. My code may be not correct, I do not know. So far, it still can run. Just cant seek the song and run when the song is played.
Jan 25, 2014 at 11:21 AM
Are you using Forms or WPF?

If WPF, you can Bind the trackbar to the Position property.

If you are using forms, you need to use a Timer and call mainOutputStream.CurrentTime to update the trackbar position value every 250ms or so.

If you download the NAudio source code and have a look at the sample apps you can see how it works.
Jan 25, 2014 at 1:07 PM
Hi K24A3, thx for the help!! I required a timer. Thx for telling me. My seekbar run perfectly after I added in a timer. Thx for the help. TQ
Apr 18, 2015 at 12:53 PM

I'm trying to do the same here. But thats not working for me.
I can Make the TrackBar to Auto Slide During Track Play but Cannot Seek.
Can anyone help me out understanding what i'm doing wrong?
nAudio Class:

        #region Member Variables
        public WaveOutEvent WOE;
        public AudioFileReader AFR;

 #region [TrackBar]
        /* TrackBar Time and Position*/
        public int trkBar_lenght;
        public int trkBar_Lenght { get { return trkBar_lenght; } set { trkBar_lenght = value; } }

        public int trkBar_curTime;
        public int trkBar_CurTime { get { return trkBar_curTime; } set { trkBar_curTime = value; } }

        public int trkBar_curPos;
        public int trkBar_CurPos { get { return trkBar_curPos; } set { trkBar_curPos = value; } }

        public string curTime;
        public string CurTime { get { return curTime; } set { curTime = value; } }

        public void Set_CurTime()
            if (WOE == null) { curTime = "00:00"; }
            else if (WOE != null) 
                curTime = AFR.CurrentTime.ToString("mm\\:ss");
                trkBar_curTime = (int)AFR.CurrentTime.TotalSeconds;

        public string trk_lenght;
        public string trk_Lenght { get { return trk_lenght; } set { trk_lenght = value; } }

        public void Set_Lenght()
            if (WOE == null) { trk_lenght = ""; }
            else if (WOE != null) 
                trk_lenght = AFR.TotalTime.ToString("mm\\:ss");
                trkBar_lenght = (int)AFR.TotalTime.TotalSeconds;


Main Form:

        private void trkBar_Scroll(object sender, EventArgs e)
            NAUDIO.trkBar_CurTime = trkBar.Value;

        private void trkBar_ValueChanged(object sender, EventArgs e)
            trkBar.Minimum = 0;
            trkBar.Maximum = NAUDIO.trkBar_Lenght;

        private void tmr_TrkTime_Tick(object sender, EventArgs e)
            trkBar.Value = NAUDIO.trkBar_CurTime;

            txt_CurrTime.Text = NAUDIO.CurTime;
            txt_trkLenght.Text = NAUDIO.trk_Lenght;