I’m trying to play a MIDI file in my VB.NET (VB 2010 Express) and things work well with the code from this other question here on Stack Overflow, which I translated from C to VB.
However, I also need to PAUSE, while that code is only for open and stop. I edited the code like this:
Imports System.Runtime.InteropServices
Imports System.IO
''' <summary>
''' MCIPlayer is based off code by Slain.
''' Found here: http://www.sadeveloper.net/Articles_View.aspx?articleID=212
''' </summary>
Public Class MCIPlayer
Private Shared ReadOnly sAlias As String = "TeaTimerAudio"
<DllImport("winmm.dll")> _
Private Shared Function mciSendString(ByVal strCommand As String, ByVal strReturn As StringBuilder, ByVal iReturnLength As Integer, ByVal hwndCallback As IntPtr) As Long
End Function
Public Shared Sub Open(ByVal sFileName As String)
If _Status() <> "" Then
_Close()
End If
Dim sCommand As String = "open """ & sFileName & """ alias " & sAlias
mciSendString(sCommand, Nothing, 0, IntPtr.Zero)
End Sub
Public Shared Sub Close()
Dim sCommand As String = "close " & sAlias
mciSendString(sCommand, Nothing, 0, IntPtr.Zero)
End Sub
Public Shared Sub Pause()
Dim sCommand As String = "pause " & sAlias
mciSendString(sCommand, Nothing, 0, IntPtr.Zero)
End Sub
Public Shared Sub Play()
Dim sCommand As String = "play " & sAlias
mciSendString(sCommand, Nothing, 0, IntPtr.Zero)
End Sub
Public Shared Function Status() As String
Dim sBuffer As New StringBuilder(128)
mciSendString("status " & sAlias & " mode", sBuffer, sBuffer.Capacity, IntPtr.Zero)
Return sBuffer.ToString()
End Function
End Class
And I call it like this:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MCIPlayer.Open("C:\Users\User\Desktop\aqua-roses_are_red.mid")
End Sub
Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
MCIPlayer.Play()
End Sub
Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PauseButton.Click
MCIPlayer.Pause()
End Sub
Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
MCIPlayer.Close()
End Sub
The problem is that, for some reason, if I click play, then pause, then play again, the file actually resumes playing from where it was left, but the instruments are completely different. The melody switches back to the default piano, while before pressing pause it was a complete different sound.
Can you help me on this?
I’m on Win7x64
Thanks a lot! Best
OK, I figured out how to do it. Here is the code, loosely inspired to the freebasic.net link I mentioned in the comment above, but a lot simpler:
And the status procedure is modified like this:
I guess that, when a song is played from the beginning, the player reads instrument info. When it starts from a different position, you need to let the device driver know that he’s starting from a different position, so that he can retrieve the previous instrument settings.
Anyway, all’s well what ends well (although it’s not the end yet… When my app is done, I need to test it on other platforms too and see if it works the same).