|
Public Class MainForm
Private Sub OpenFile(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles OpenFileButton.Click
If OpenFileDialog.ShowDialog(Me) = _
System.Windows.Forms.DialogResult.OK Then
LengthLabel.Text = "Länge: " & _
GetWaveLength(OpenFileDialog.FileName).ToString()
End If
End Sub
Private Function GetWaveLength(ByVal path As String) As System.TimeSpan
Using device As New MediaDevice(path)
Return device.Length
End Using
End Function
End Class
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Public Class MediaDevice
Implements IDisposable
Private Const mObjectName As String = "MediaDevice"
Private Declare Auto Function mciGetErrorString Lib "winmm.dll" ( _
ByVal fdwError As UInteger, _
ByVal lpszErrorText As StringBuilder, _
ByVal cchErrorText As Integer) As Boolean
Private Declare Auto Function mciSendCommand Lib "winmm.dll" ( _
ByVal wDeviceID As UInteger, _
ByVal uMsg As UInteger, _
ByVal dwFlags As UInteger, _
ByRef lpOpen As MCI_WAVE_OPEN_PARMS) As UInteger
Private Declare Auto Function mciSendCommand Lib "winmm.dll" ( _
ByVal wDeviceID As UInteger, _
ByVal uMsg As UInteger, _
ByVal dwFlags As UInteger, _
ByRef lpClose As MCI_CLOSE_PARMS) As UInteger
Private Declare Auto Function mciSendCommand Lib "winmm.dll" ( _
ByVal wDeviceID As UInteger, _
ByVal uMsg As UInteger, _
ByVal dwFlags As UInteger, _
ByRef lpStatus As MCI_STATUS_PARMS) As UInteger
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure MCI_WAVE_OPEN_PARMS
Public dwCallback As UIntPtr
Public wDeviceID As UInteger
Public lpstrDeviceType As UIntPtr
Public lpstrElementName As String
Public lpstrAlias As UIntPtr
Public dwBufferSeconds As UInteger
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Private Structure MCI_CLOSE_PARMS
Public dwCallback As UIntPtr
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Private Structure MCI_STATUS_PARMS
Public dwCallback As UIntPtr
Public dwReturn As UInteger
Public dwItem As UInteger
Public dwTrack As UInteger
End Structure
Private Const mErrorTextLength As Integer = 128
Private Const MCI_OPEN As UInteger = &H803UI
Private Const MCI_CLOSE As UInteger = &H804UI
Private Const MCI_STATUS As UInteger = &H814UI
Private Const MCI_WAIT As UInteger = &H2UI
Private Const MCI_TRACK As UInteger = &H10UI
Private Const MCI_STATUS_ITEM As UInteger = &H100UI
Private Const MCI_OPEN_ELEMENT As UInteger = &H200UI
Private Const MCI_OPEN_TYPE_ID As UInteger = &H1000UI
Private Const MCI_OPEN_TYPE As UInteger = &H2000UI
Private Const MCI_DEVTYPE_WAVEFORM_AUDIO As UInteger = 522UI
Private Const MCI_STATUS_LENGTH As UInteger = &H1UI
Private mDisposed As Boolean
Private mOpen As Boolean
Private mDevice As UInteger
Public Sub New(ByVal path As String)
Open(path)
End Sub
Public Sub Open(ByVal path As String)
If mDisposed Then Throw New ObjectDisposedException(mObjectName)
If mOpen Then Close()
Dim Params As MCI_WAVE_OPEN_PARMS
Params.lpstrDeviceType = New UIntPtr(MCI_DEVTYPE_WAVEFORM_AUDIO)
Params.lpstrElementName = path
If CheckResult(mciSendCommand(0, MCI_OPEN, MCI_WAIT Or _
MCI_OPEN_ELEMENT Or MCI_OPEN_TYPE Or MCI_OPEN_TYPE_ID, Params)) Then
mOpen = True
mDevice = Params.wDeviceID
End If
End Sub
Public Sub Close()
If mOpen Then
Dim Params As MCI_CLOSE_PARMS
If CheckResult(mciSendCommand(mDevice, MCI_CLOSE, _
MCI_WAIT, Params)) Then
mOpen = False
mDevice = 0
End If
End If
End Sub
Public ReadOnly Property Length() As TimeSpan
Get
If mDisposed Then Throw New ObjectDisposedException(mObjectName)
Dim Params As MCI_STATUS_PARMS
Params.dwItem = MCI_STATUS_LENGTH
Params.dwTrack = 1
If CheckResult(mciSendCommand(mDevice, MCI_STATUS, _
MCI_WAIT Or MCI_TRACK Or MCI_STATUS_ITEM, Params)) Then
Return TimeSpan.FromMilliseconds(Params.dwReturn)
End If
End Get
End Property
Private Function CheckResult(ByVal result As UInteger) As Boolean
If result = 0 Then
Return True
Else
Dim builder As New StringBuilder(mErrorTextLength)
If mciGetErrorString(result, builder, mErrorTextLength) Then
Throw New MediaException(builder.ToString())
Else
Throw New MediaException()
End If
End If
End Function
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not mDisposed Then
Close()
mDisposed = True
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
Imports System
Imports System.Text
Public Class MediaException
Inherits Exception
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class |