0

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;
giiYanJ
  • 3
  • 2
  • 8

1 Answers1

1

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

Community
  • 1
  • 1
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • CurrentTime.TotalSeconds is 'double', and there's decimal behind, anyway to remove it? – giiYanJ Dec 03 '12 at 00:49
  • opps, sorry I forgot to cast as int, fixed above – sa_ddam213 Dec 03 '12 at 00:50
  • woah, worked like god! How about the duration part? mainOutputStream.Length only gives the bytes, any way to calculate the duration but not average bytes/sec? EDIT: Just found out that there's an `TotalTime` object there, a timespan, good. – giiYanJ Dec 03 '12 at 00:54
  • erm... got a little prob. `string totalTime = mainOutputStream.TotalTime.TotalSeconds.ToString("ss");` `int totalTimeSec = int.Parse(totalTime);` This two don't work... – giiYanJ Dec 03 '12 at 01:04
  • `mainOutputStream.TotalTime.TotalSeconds.ToString()` you dont need the"ss" format for TotalSeconds – sa_ddam213 Dec 03 '12 at 01:06