1

I'm writing a CD Audio player program using MCI, but I can't show the progress of the audio file on a trackbar.

Does anyone know how?

Note that I must use mciSendString to get the track length.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Arshtat
  • 53
  • 6

2 Answers2

0

From Simple MCI Player - CodeProject, slightly altered:

public int GetCurrentPosition()
{
    String command = "status MediaFile position";
    error = mciSendString(command, returnData, 
                          returnData.Capacity, IntPtr.Zero);
    return error == 0 ? int.Parse(returnData.ToString()) : 0;
}

public int GetSongLenght()
{
    if (IsPlaying())
    {
        String command = "status MediaFile length";
        error = mciSendString(command, returnData, returnData.Capacity, IntPtr.Zero);
        return error == 0 ? int.Parse(returnData.ToString()) : 0;
    }
    else
        return 0;
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • thx but my question here is about the trackbar...i don't know how to show the track progress on it... – Arshtat Oct 12 '12 at 11:07
  • @user1740616 call `GetCurrentPosition` to get the current position of the file, then set the trackbar's value to that. – CodeCaster Oct 12 '12 at 11:19
  • how about eventhandler? i think i need that too? – Arshtat Oct 12 '12 at 11:44
  • @user1740616 what makes you think so? Does MCI raise events? – CodeCaster Oct 12 '12 at 11:52
  • ok so after i get the current position i pass it to trackbars value ok i have done that,but its still not moving with track..so i think i need an eventhandler... – Arshtat Oct 12 '12 at 11:59
  • @user1740616 you don't need an eventhandler, because there is no event to be handled. You could use a timer that polls the track location every second or so. – CodeCaster Oct 12 '12 at 12:09
  • yea thats the problem ...how? lol sorry its just that i didnt use c# in a long time... – Arshtat Oct 12 '12 at 12:17
  • thx alot but i already tried that..well i think i'll try and figure it out myself from here.thx again – Arshtat Oct 12 '12 at 12:49
  • @CodeCaster How do I convert the number returned from `GetSongLenght()` to seconds? – lbrahim Sep 20 '15 at 17:09
0

In VB I did this inside a Timer Tick Sub...easy peasy really...

rem audio is the mcisendstring thingy rem TotalLength is total seconds of current track

    Dim PlayPosition As Long = 0

    PlayPosition = audio.SecondsPlayed
    If PlayPosition > 0 And PlayPosition < TotalLength Then
        TrackBar1.Value = (PlayPosition / TotalLength) * TotalLength
    End If