这种方法只在开发过程中对我很有效,但是当我通过 mcisendstring 发出打开命令时,我安装的大多数计算机都会崩溃。我还没弄清楚为什么。这是我的代码。也许它会帮助某人,也许有人可以弄清楚我做错了什么。我在从 64 位开发机器上运行 32 位应用程序时遇到问题。
Imports System.Runtime.InteropServices
Imports System.Text
Public Class MediaPlayerClass
<DllImport("winmm.dll")> _
Private Shared Function mciSendString(ByVal command As String, ByVal buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer
End Function
<DllImport("winmm.dll")> _
Private Shared Function mciGetErrorString(errCode As Integer, ByVal errMsg As StringBuilder, bufferSize As Integer) As Integer
End Function
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function GetShortPathName(ByVal longPath As String, _
<MarshalAs(UnmanagedType.LPTStr)> ByVal ShortPath As System.Text.StringBuilder, _
<MarshalAs(Runtime.InteropServices.UnmanagedType.U4)> ByVal bufferSize As Integer) As Integer
End Function
Private _filename As String
Private _MediaAlias As String
Private _Length As TimeSpan
Private _err As Integer
Public Property PlaylistId As Integer = 0
Private _OriginalVolume As Integer = 1000
Function ShortPathName(ByVal Path As String) As String
Dim sb As New System.Text.StringBuilder(1024)
Dim tempVal As Integer = GetShortPathName(Path, sb, 1024)
If tempVal <> 0 Then
Dim Result As String = sb.ToString()
Return Result
Else
Throw New Exception("Failed to return a short path")
End If
End Function
Public Sub New(Filename As String, MediaAlias As String)
_filename = ShortPathName(Filename)
_MediaAlias = MediaAlias.Replace(" ", "_")
'_Length = GetLength()
Try
My.Application.Log.WriteEntry("MediaPlayerClass.New - calling MCI OPEN")
' here is where it crashes
_err = mciSendString("open """ & _filename & """ alias " & MediaAlias, Nothing, 0, 0)
Catch ex As Exception
MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
End Try
End Sub
Public Sub NewMP3(Filename As String)
Me.StopIt()
Me.CloseIt()
_filename = Filename
Try
My.Application.Log.WriteEntry("MediaPlayerClass.NewMP3 - calling MCI OPEN ")
_err = mciSendString("open """ & Filename & """ alias " & _MediaAlias, Nothing, 0, 0)
Catch ex As Exception
MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
End Try
End Sub
Public ReadOnly Property Length As TimeSpan
Get
Return _length
End Get
End Property
Private Function GetLength() As TimeSpan
Dim lengthBuf As New StringBuilder(32)
Try
My.Application.Log.WriteEntry("MediaPlayerClass.GetLength - calling MCI OPEN")
_err = mciSendString("open """ & _filename & """ type waveaudio alias " & _MediaAlias, Nothing, 0, 0)
Catch ex As Exception
MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
End Try
' Get the duration of the music
Try
_err = mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, 0)
Catch ex As Exception
MsgBox(ex.ToString & vbCrLf & GetLastErrorMessage())
End Try
'mciSendString("close wave", Nothing, 0, 0)
Dim len As Integer = Integer.TryParse(lengthBuf.ToString, len)
Dim ts As TimeSpan = TimeSpan.FromMilliseconds(len)
Return ts
End Function
Public Function PlayIt(Optional WaitUntilFinishedPlaying As Boolean = False) As Integer
Try
My.Application.Log.WriteEntry("MediaPlayerClass.PlayIt - calling MCI PLAY")
_err = mciSendString("play " & _MediaAlias, Nothing, 0, IntPtr.Zero)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
While WaitUntilFinishedPlaying
If IsPlaying() Then
Threading.Thread.Sleep(250)
Else
Exit While
End If
End While
Return _err
End Function
Public Function PauseIt() As Integer
_err = mciSendString("pause " & _MediaAlias, Nothing, 0, IntPtr.Zero)
Return _err
End Function
Public Function ResumeIt() As Integer
_err = mciSendString("resume " & _MediaAlias, Nothing, 0, IntPtr.Zero)
Return _err
End Function
Public Function StopIt() As Boolean
_err = mciSendString("stop " & _MediaAlias, Nothing, 0, IntPtr.Zero)
Return _err
End Function
Public Function CloseIt() As Boolean
_err = mciSendString("close " & _MediaAlias, Nothing, 0, IntPtr.Zero)
Return _err
End Function
Public Function IsPlaying() As Boolean
Dim returnData As New StringBuilder(128)
_err = mciSendString("status " & _MediaAlias & " mode", returnData, 128, IntPtr.Zero)
Return (returnData.Length = 7 AndAlso returnData.ToString.Substring(0, 7) = "playing")
End Function
Public Function SetVolume(vol As Integer) As Integer
_err = -1
If vol >= 0 And vol <= 1000 Then
_err = mciSendString("setaudio " & _MediaAlias & " volume to " & vol.ToString, Nothing, 0, IntPtr.Zero)
End If
Return _err
End Function
Public Sub FadeOutAndPause()
_OriginalVolume = GetVolume()
For x As Integer = 30 To 1 Step -1
Me.SetVolume(Int(x / 30 * _OriginalVolume))
Threading.Thread.Sleep(100)
Next
Me.PauseIt()
End Sub
Public Sub PlayAndFadeIn()
Me.PlayIt()
For x As Integer = 1 To 30 Step 1
Me.SetVolume(Int(x / 30 * _OriginalVolume))
Threading.Thread.Sleep(100)
Next
End Sub
Public Function GetVolume() As Integer
Dim returnData As New StringBuilder(128)
_err = mciSendString("status " & _MediaAlias & " volume", returnData, 128, IntPtr.Zero)
'MsgBox(returnData.ToString)
If _err = 0 Then
Return CInt(returnData.ToString)
Else
Return 1000
End If
End Function
Public Function SetBalance(bal As Integer) As Integer
If bal >= 0 AndAlso bal <= 1000 Then
_err = mciSendString("setaudio " & _MediaAlias & " left volume to " + (1000 - bal).ToString, Nothing, 0, IntPtr.Zero)
_err = mciSendString("setaudio " & _MediaAlias & " right volume to " + bal.ToString, Nothing, 0, IntPtr.Zero)
End If
Return _err
End Function
Public Function GetLastErrorMessage() As String
Dim returnData As New StringBuilder(128)
_err = mciGetErrorString(_err, returnData, 128)
Return returnData.ToString.Trim
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
CloseIt()
End Sub
End Class