Finding the duration of a Windows Media File
So, having spent the last 4-5 hours trying to find out how to extract the duration from a Windows Media File, I finally found a way to do it.
I have been through a lot – even trying to figure out how to convert at QWORD into somthing readable – but without luck.
Finally i stumbled upon a post in the forum on http://www.codeproject.com (excellent site) – which enabled me to create this function:
| ''' <summary> |
| ''' Finds the duration of a specified windows media file using |
| ''' a Windows Media Player Object |
| ''' </summary> |
| ''' <param name="MediaFile">MediaFila as reference to the file</param> |
| ''' <returns>Double containing the length</returns> |
| ''' <remarks></remarks> |
| Private Function FindDurationWMF(ByVal MediaFile As String) As Double |
| Try |
| Dim myWMP As WindowsMediaPlayer = New WindowsMediaPlayer |
| Dim str As String = MediaFile |
| Dim duration As Double |
| myWMP.URL = str |
| myWMP.currentMedia.name = str |
| duration = myWMP.currentMedia.duration |
| myWMP.close() |
| Return duration |
| Catch ex As Exception |
| Throw New Exception(ex.Message, ex.InnerException) |
| End Try |
| End Function |
Remember to add a reference to the Windows Media Player DLL, and to import it at the top of your class.
The project I’m currently working on, is cutting a media-file up into smaller pieces (with shorter duration) using the Basic Edit function of Windows Media Encoder. The actual problem was, that the out-point specified in some situations ware just e.g. 100 miliseconds longer than the actual file, which caused the appliction to break.
Technorati: Windows+Media+Player+Object, duration, Windows+Media+File