1

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

Community
  • 1
  • 1
Davide
  • 87
  • 13
  • Apparently, someone had the same problem and was able to solve it: [http://www.freebasic.net/forum/viewtopic.php?p=23358&sid=937c1f6c627fec492be6eb08f837fa55][1] I'll check it out tomorrow. Thanks everyone! [1]: http://www.freebasic.net/forum/viewtopic.php?p=23358&sid=937c1f6c627fec492be6eb08f837fa55 – Davide Nov 19 '11 at 22:47

1 Answers1

0

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:

Public Shared Sub ResumeAfterPause()
    MsgBox(CLng(_Status()))   'Just to make sure the status is the position, see below
    Dim sCommand As String = "play " & sAlias & " from " & CLng(_Status())
    mciSendString(sCommand, Nothing, 0, IntPtr.Zero)
End Sub

And the status procedure is modified like this:

Public Shared Function _Status() As String
    Dim sBuffer As New StringBuilder(128)
    mciSendString("status " & sAlias & " position", sBuffer, sBuffer.Capacity, IntPtr.Zero)
    Return sBuffer.ToString()
End Function

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).

Davide
  • 87
  • 13