Hi I have a working media player that references the imported winmm.dll. I am trying to link a trackbar to the dll to find the file length & seek to the scrolled related timeframe. I have the code now that only starts the trackbar when the play button starts. Any help would be awsome! Thanks,

Jamesonh20

using System.Runtime.InteropServices;
using System.IO;
//Import DLL
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
private void trackBar1_Scroll(object sender, EventArgs e)
        {
            //Converts trackbar value to label
            string trackerbar = Convert.ToString(scrollTimer.Interval);
            label2.Text = trackBar1.Value.ToString();                              
        }
private void scrollTimer_Tick_1(object sender, EventArgs e)
        {
            if (trackBar1.Value < trackBar1.Maximum)
            {
                label2.Text = trackBar1.Value.ToString();
                trackBar1.Value += 1;
                
            }
            else
            {
                trackBar1.Value = 0;
                scrollTimer.Enabled = false;
            }
    
        }

Recommended Answers

All 8 Replies

an easier way it to use the Microsoft.directX.audiovideoplayback.audio class It provides a current position property and a length property that makes it very easy.

an easier way it to use the Microsoft.directX.audiovideoplayback.audio class It provides a current position property and a length property that makes it very easy.

Okay so I have heard of the directx dll. Can I use both of them in one player(i.e. import winmm.dll and directx), or do I have to re-write my code? I'm new to this, so just leaving me a quote saying look at MSDN library dosent get me very far...LOL. All im trying to do is post the play time, and seek to a label next to my trackbar on my GUI interface.

Jameson'

Okay so I have managed to code the trackBar1 to seek and relay the audiolength to my label2 control. But the problem I run into is that everything is in seconds only. I need to convert the play time to hrs:min:sec. Anyone care to shout out an example?

Jamesonh20'

1000 ms is 1 sec. so divide by 1000 you get seconds, divide seconds by 60 you get minutes, the remainder is left over seconds. So if you were interested in doing the math on the fly you could go

//assuming Milliseconds is a var holding the current value in milliseconds to be converted.
string Hours = Milliseconds / (1000*60*60)
string Minutes = (Milliseconds % (1000*60*60)) / (1000*60)
string Seconds = ((Milliseconds % (1000*60*60)) % (1000*60)) / 1000

or you could use the TimeSpan class as follows

TimeSpan t = TimeSpan.FromSeconds(80);
string Position = t.ToString();

Now please do some research before you ask simple questjions like this. It will stick with you longer and you will grow faster as a programmer if you take the time to learn how to solve a problems on your own.

The forum is best used to get you un stuck when you have been stuck or my personal favorite, for discussions, as I love to know how someone else would have done it.

1000 ms is 1 sec. so divide by 1000 you get seconds, divide seconds by 60 you get minutes, the remainder is left over seconds. So if you were interested in doing the math on the fly you could go

//assuming Milliseconds is a var holding the current value in milliseconds to be converted.
string Hours = Milliseconds / (1000*60*60)
string Minutes = (Milliseconds % (1000*60*60)) / (1000*60)
string Seconds = ((Milliseconds % (1000*60*60)) % (1000*60)) / 1000

or you could use the TimeSpan class as follows

TimeSpan t = TimeSpan.FromSeconds(80);
string Position = t.ToString();

Now please do some research before you ask simple questjions like this. It will stick with you longer and you will grow faster as a programmer if you take the time to learn how to solve a problems on your own.

The forum is best used to get you un stuck when you have been stuck or my personal favorite, for discussions, as I love to know how someone else would have done it.

Thank you so much for your help, I knew about the 1000ms --> 1sec, but I was trying to do it by the horribly wrong method of:
if (trackBar1.value >=60)
{...
LOL
Thanks again!!
Jamesonh20'

So roughly this is what worked for me:

private void scrollTimer_Tick(object sender, EventArgs e)
        {
            if (trackBar1.Value < trackBar1.Maximum)
            {
                label2.Text = trackBar1.Value.ToString();
                string trackBar = Convert.ToString(scrollTimer.Interval);
                trackBar1.Value += 1;
                trackBar1.Value = (int)(this.CurrentPosition / 1000);
            }
            else
            {
                trackBar1.Value = 0;
                scrollTimer.Enabled = false;
            }            
        }
 public void Seek(ulong Millisecs)
        {
            if (isOpen && Millisecs <= Lng)
            {
                    if (Pause)
                    {
                        _command = String.Format("seek MediaFile to {0}", Millisecs);
                        if ((Err = mciSendString(_command, null, 0, IntPtr.Zero)) != 0) OnError(new ErrorEventArgs(Err));
                    }
                    else
                    {
                        _command = String.Format("seek MediaFile to {0}", Millisecs);
                        if ((Err = mciSendString(_command, null, 0, IntPtr.Zero)) != 0) OnError(new ErrorEventArgs(Err));
                        _command = "play MediaFile";
                        if ((Err = mciSendString(_command, null, 0, IntPtr.Zero)) != 0) OnError(new ErrorEventArgs(Err));
                    }
                }
            }
        

        private void CalculateLength()
        {
            StringBuilder str = new StringBuilder(128);
            mciSendString("status MediaFile length", str, 128, IntPtr.Zero);
            Lng = Convert.ToUInt64(str.ToString());
        }

        public ulong AudioLength
        {
            get
            {
                if (isOpen) return Lng;
                else return 0;
            }
        }

        public ulong CurrentPosition
        {
            get
            {
                if (isOpen)
                {
                    StringBuilder s = new StringBuilder(128);
                    _command = "status MediaFile position";
                    if ((Err = mciSendString(_command, s, 128, IntPtr.Zero)) != 0) OnError(new ErrorEventArgs(Err));
                    return Convert.ToUInt64(s.ToString());
                }
                else return 0;
            }
        }

there were some other minor statements but this was the bulk of it... Hope it helps someone else one day!

Cheers'
Jamesonh20'

jamesonh20 I have a problem with you code :(
you could not show the project please....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.