Question

I'm working with a player with NAudio, refer to http://naudio.codeplex.com/wikipage?title=MP3. Everything worked fine but when I'm coding a trackbar, which show the current time and duration, even can seek into any position of the song, so I used timer to update the current time with mainOutputStream.CurrentTime,but the format is bad to look, then I reformatted it like this:

string curTime = mainOutputStream.CurrentTime.ToString();
string[] format = curTime.Split(':');
format[2] = format[2].Remove(2);
curTime = format[1] + format[2];

but the format[2] = format[2].Remove(2); always return an error. And seems there's no way to get the duration even in timespan but bytes, but I don't like using averageBytePerSecond or anything like this so is there anyway to do these jobs simpler and better? All variable as in the reference above

Thanks.

EDIT: Problems solved~ I used this method:
Get Duration:

string duration = mainOutputStream.TotalTime.ToString("mm\\:ss");
trackPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;

Get Current Time:

string curTime = mainOutputStream.CurrentTime.ToString("mm\\:ss");
trackPosition.Value = (int)mainOutputStream.CurrentTime.TotalSeconds;
Was it helpful?

Solution

Try this:

string curTimeString = mainOutputStream.CurrentTime.ToString("mm\\:ss");
int curTimeSeconds =  (int)mainOutputStream.CurrentTime.TotalSeconds;

the first one is only formated for minutes and seconds, if your audio tracks are larger you will have to add different formatting.

More info on that can be found here: Timespan formatting

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top