Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This method has worked well for me only during development but most computers I installed on would crash when i would issue the open command via mcisendstring. I haven't figured out why. Here's my code. Maybe it will help someone and maybe someone can figure out what I'm doing wrong. I've had problems getting 32 bit apps to run from my 64 bit development machine.</p> <pre><code>Imports System.Runtime.InteropServices Imports System.Text Public Class MediaPlayerClass &lt;DllImport("winmm.dll")&gt; _ Private Shared Function mciSendString(ByVal command As String, ByVal buffer As StringBuilder, ByVal bufferSize As Integer, ByVal hwndCallback As IntPtr) As Integer End Function &lt;DllImport("winmm.dll")&gt; _ Private Shared Function mciGetErrorString(errCode As Integer, ByVal errMsg As StringBuilder, bufferSize As Integer) As Integer End Function &lt;DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)&gt; _ Public Shared Function GetShortPathName(ByVal longPath As String, _ &lt;MarshalAs(UnmanagedType.LPTStr)&gt; ByVal ShortPath As System.Text.StringBuilder, _ &lt;MarshalAs(Runtime.InteropServices.UnmanagedType.U4)&gt; 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 &lt;&gt; 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 """ &amp; _filename &amp; """ alias " &amp; MediaAlias, Nothing, 0, 0) Catch ex As Exception MsgBox(ex.ToString &amp; vbCrLf &amp; 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 """ &amp; Filename &amp; """ alias " &amp; _MediaAlias, Nothing, 0, 0) Catch ex As Exception MsgBox(ex.ToString &amp; vbCrLf &amp; 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 """ &amp; _filename &amp; """ type waveaudio alias " &amp; _MediaAlias, Nothing, 0, 0) Catch ex As Exception MsgBox(ex.ToString &amp; vbCrLf &amp; 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 &amp; vbCrLf &amp; 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 " &amp; _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 " &amp; _MediaAlias, Nothing, 0, IntPtr.Zero) Return _err End Function Public Function ResumeIt() As Integer _err = mciSendString("resume " &amp; _MediaAlias, Nothing, 0, IntPtr.Zero) Return _err End Function Public Function StopIt() As Boolean _err = mciSendString("stop " &amp; _MediaAlias, Nothing, 0, IntPtr.Zero) Return _err End Function Public Function CloseIt() As Boolean _err = mciSendString("close " &amp; _MediaAlias, Nothing, 0, IntPtr.Zero) Return _err End Function Public Function IsPlaying() As Boolean Dim returnData As New StringBuilder(128) _err = mciSendString("status " &amp; _MediaAlias &amp; " 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 &gt;= 0 And vol &lt;= 1000 Then _err = mciSendString("setaudio " &amp; _MediaAlias &amp; " volume to " &amp; 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 " &amp; _MediaAlias &amp; " 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 &gt;= 0 AndAlso bal &lt;= 1000 Then _err = mciSendString("setaudio " &amp; _MediaAlias &amp; " left volume to " + (1000 - bal).ToString, Nothing, 0, IntPtr.Zero) _err = mciSendString("setaudio " &amp; _MediaAlias &amp; " 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload