我正在尝试在我的VB.NET(VB 2010 Express)中播放MIDI文件,并且使用this other question here on Stack Overflow中的代码可以很好地工作,我将其从C转换为VB。
但是,我还需要PAUSE,而该代码仅用于打开和停止。我编辑了这样的代码:
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
我称之为:
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
问题在于,出于某种原因,如果我单击播放,然后暂停,然后再次播放,文件实际上将从剩下的位置继续播放,但乐器完全不同。旋律切换回默认钢琴,而在按下暂停之前,它是完全不同的声音。
你能帮我解决这个问题吗?
我在Win7x64上
非常感谢!最好
答案 0 :(得分:0)
好的,我想出了怎么做。这是代码,松散地启发我在上面的评论中提到的freebasic.net链接,但更简单:
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
状态过程修改如下:
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
我想,当从一开始播放歌曲时,播放器会读取乐器信息。当它从不同的位置开始时,您需要让设备驱动程序知道他从不同的位置开始,以便他可以检索以前的仪器设置。
无论如何,一切都很好(尽管还不是结束......当我的应用程序完成后,我还需要在其他平台上测试它,看看它是否也能正常工作)。