Can not change audio position with trackbar in C#


December 2018


478 time


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

2 answers


есть проблема в вашем конверсионной стоимости трек-бар времени. ваш код возврата микросекунд:

Timespan newPos = new Timespan(SongProgressTrackbar.Value);

Вы должны использовать так: (это возвращение секунд)

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

этот код преобразование TrackBar в секунды. Благодарю.


вы должны преобразовать ваши миллисекунды в Tick. Один тик представляет собой одну сотню наносекунд или одну десятимиллионной доли секунды. Есть 10000 тиков в миллисекунду, или 10 миллионов тиков в секунду. Одно Argumant использование TimeSpan получить значение, как клещ.

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