2

I am working in a small audio player. You know, with the simplest stuff and functions.

I am having an issue when changing the trackbar's value (scrolling), this trackbar displays the current audio position.

When I drag the trackbar's slider, audio position should change. I am using the CSCore Audio Library. This is the code I use to update the current position of the audio playing:

private void ProgressTimerTick(object sender, EventArgs e)
{
SongProgressTrackbar.MaxValue = (int)audioPlayer.Length.TotalMilliseconds;
SongProgress.Value = (int)audioPlayer.Position.TotalMilliseconds;
}

This works perfectly with no issues.

But this...

private void SongProgressTrackbar_Scroll(object sender, ScrollEventArgs e)
{
Timespan newPos = new Timespan(SongProgressTrackbar.Value);
audioPlayer.Position = newPos;
}

What happens here, is that the audio goes to the start again when the event is fired, insteqd of changing the position to where I click in the trackbar, it begins again.

So, what did I write wrong? I tried using the ValueChanged Event, but that just gets the audio stay in 0:00 position. As I said, the TimerTick does work. But whenever I scroll the trackbar, the audio starts again from the beginning.

Hope someone can help me solve this issue. Thanks in Advance - CCB

ChrisCreateBoss
  • 323
  • 7
  • 21
  • You have an obvious unit conversion issue, right now you set the time in units of 100 nanoseconds. Close enough to 0. Use TimeSpan.FromMilliseconds() instead. – Hans Passant Apr 28 '16 at 07:12
  • Thanks Hans ! Timespan.FromMilliseconds did work! Please post your comment as solution to mark it. – ChrisCreateBoss May 02 '16 at 00:11

2 Answers2

1

there is a problem in your conversion track-bar value to time. your code return microseconds :

Timespan newPos = new Timespan(SongProgressTrackbar.Value);

you should be use like this : ( this return seconds )

Timespan newPos = new Timespan(0,0,SongProgressTrackbar.Value);

this code convert trackbar to seconds. thanks.

  • My timer has an interval of 100 milliseconds. That's why I am using TotalMilliseconds. This solution can convert my timespan to seconds, but that's not what I am looking for. But thanks anyways. – ChrisCreateBoss Apr 28 '16 at 06:28
1

you should convert your millisecond to Tick. A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second. Single Argumant use of TimeSpan get values as Tick.

Timespan newPos = new Timespan(SongProgressTrackbar.Value * 10000);

thanks.