2

I am trying to get the Duration of the video that i have loaded into my Windows Media Player but it seems to be returning back '0.0'

I need the duration value before the video starts playing as the video length determines the width of a slide.

This is how i am trying to do it at the moment:

timeElapsed = AxWindowsMediaPlayer_OnDemand.currentMedia.duration - marker.SyncTime

I have also tried this which returns an empty string:

Dim asas As String = AxWindowsMediaPlayer_OnDemand.currentMedia.getItemInfo("Duration")

Does anyone have any idea of how to get the length of the video loaded in?

Ben Clarke
  • 1,051
  • 4
  • 21
  • 47

1 Answers1

2

Add a reference to Windows Media Player

Use the follow 2 lines in your function to call it

Dim VidSecs As Integer = Math.Round(GetMediaDuration(YourFilePath))'Get total seconds
Dim Vidhhmmss As String = TimeSpan.FromSeconds(Math.Round(GetMediaDuration(YourFilePath))).ToString ' Format hh:mm:ss


Public Function GetMediaDuration(ByVal MediaFile As String)
        Try
            Dim w As New WMPLib.WindowsMediaPlayer
            Dim m As WMPLib.IWMPMedia = w.newMedia(MediaFile)
            w.close()
            Return m.duration
        Catch ex As Exception
            Return 0
        End Try
    End Function
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
Mike
  • 21
  • 2