Results 1 to 12 of 12

Thread: [RESOLVED] [2008] Get playing time of a media file without using WMP

  1. #1

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Resolved [RESOLVED] [2008] Get playing time of a media file without using WMP

    I'm using mciSendString to play a media file inside my form, however I'd like to be able to hide the form after the media has finished playing as well as showing the playing time of the video. Is there a way I can get the playing time of the media file and get an event raised when it finishes playing? I don't want to add another dependency in the form of the WMP component.

    I found some code via google, but I can't make heads or tails of it. I don't have much experience with APIs but from what I tried it only returned zero. Here it is:
    Code:
    Function MCIWndGetLength (hwnd As HWND) As Long 
    MCIWndGetLength = SendMessage(hwnd, MCIWNDM_GETLENGTH, 0, 0)
    End Function
    
    'Found on http://translate.google.co.uk/translate?hl=en&sl=ja&u=http://www.activebasic.com/forum/viewtopic.php%3Fp%3D9024&sa=X&oi=translate&resnum=10&ct=result&prev=/search%3Fq%3DMCIWndGetLength%2B.Net%26start%3D10%26hl%3Den%26lr%3Dlang_en%26sa%3DN%26as_qdr%3Dall
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] Get playing time of a media file without using WMP

    What file type?
    VB 2005, Win Xp Pro sp2

  3. #3

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Get playing time of a media file without using WMP

    Doesn't an all-in-one solution exist? I'd prefer to make it work with all video file types, but if that is too difficult then I guess avi/wmv will have to do.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Get playing time of a media file without using WMP

    You should go to MSDN and start reading about Windows Multimedia Control Interface (MCI) and, in particular, the mciSendString function.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2008] Get playing time of a media file without using WMP

    Nevermind, it crashes randomly with different types of media.
    Last edited by Half; Jan 5th, 2009 at 12:05 AM.
    VB 2005, Win Xp Pro sp2

  6. #6
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Get playing time of a media file without using WMP

    You can use mciSendString to get the file playing length and also the position it is at. You could use a timer to keep checking the position against the length.

  7. #7

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Get playing time of a media file without using WMP

    Quote Originally Posted by jmcilhinney
    You should go to MSDN and start reading about Windows Multimedia Control Interface (MCI) and, in particular, the mciSendString function.
    Boy, I knew you'd say that. In fact I did search MSDN first, unfortunately I am not at all apt with using APIs. I found these two articles which should be related to my problem, however I am at a loss on how to use them:

    MCIWndGetLength

    MCIWNDM_GETLENGTH

    The first one is a macro and the second one is a message. So I haven't got a clue how to use the first one. As for the second one I assume that SendMessage is used, however, having never used it before, I failed.

    Quote Originally Posted by user_name
    You can use mciSendString to get the file playing length and also the position it is at.
    Would you mind providing an example?
    Last edited by obi1kenobi; Jan 5th, 2009 at 06:12 AM.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Get playing time of a media file without using WMP

    I suggested that you read up on MCI because there's a lot more to it than we can tell you here. As for the mciSendString function, it's pretty simple stuff... in principle.

    If you don't know how calling API functions works then that's the first thing you should address. Again, pretty simple stuff. You simply declare an external function with using VB syntax with a signature that's compatible with the exported C/C++ function. You can do that with the API Viewer or at PInvoke.net. This:
    vb.net Code:
    1. Declare Ansi Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal command As String, ByRef buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hWndCallback As IntPtr) As Integer
    is from the latter.

    Once the managed method signature has been created you can call it like any other method.

    The next thing to do is go to MSDN and read the Win32 documentation to find out what the function does and what each parameter means. Once you know all that, then you can think about calling the function.

    The trick with mciSendString is to know exactly what string to send to accomplish your desired task. Google can help you there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Get playing time of a media file without using WMP

    I know how declaring and calling API functions works. I said inapt, not illiterate I just don't know what to pass and how to consume the result. You could say I know the theory but have no practical experience and you wouldn't be far off.

    Can an API function be declared in multiple ways (like overloads)? Since I'm using this Declare statement:
    Code:
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    I'm playing a video on the form and it works fine with that declaration.

    I found a list (a rather length one, I might add) containing the Multimedia Command Strings. I'm doing my best to solve the issue myself, but as the list is huge I'd be very grateful if those who know the exact string to use speak up.
    Last edited by obi1kenobi; Jan 5th, 2009 at 07:12 AM.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  10. #10

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Get playing time of a media file without using WMP

    I found the most likely command, it's the status command. It has a length flag which should get the length of the file according to the MSDN documentation.

    This is the code I wrote to use it, however it didn't work:
    Code:
            mciSendString("set VideoFile time format ms", Nothing, 0, 0)  'VideoFile is the file I'm playing
            Dim str As New System.Text.StringBuilder
            Dim i As Integer = mciSendString("status MediaFile length", str, 0, 0)
    Both i and str are empty. What am I doing wrong?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  11. #11
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Get playing time of a media file without using WMP

    You need to supply a buffered string for the returned string

    Try this.
    Code:
    Dim str As String = New String(CChar(" "), 128)
    
    mciSendString("status MediaFile length", str, 128, 0)
    
    Dim i as Integer = Integer.Parse(str)

  12. #12

    Thread Starter
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: [2008] Get playing time of a media file without using WMP

    Thank you, it works like a charm. I don't think I could have figured it out by myself. Much appreciated.

    Thanks to everyone who participated in this thread.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width