VB.NET 中的 MIDI 声音:暂停后&继续,听起来不一样
我正在尝试在 VB.NET (VB 2010 Express) 中播放 MIDI 文件,并且与 Stack Overflow 上的另一个问题,我将其从 C 翻译成 VB。
但是,我还需要暂停,而该代码仅用于打开和停止。我这样编辑代码:
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
非常感谢!最好的
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我知道该怎么做了。这是代码,大致受我在上面的评论中提到的 freebasic.net 链接的启发,但要简单得多:
状态过程修改如下:
我猜,当从头开始播放歌曲时,播放器会读取仪器信息。当它从不同的位置开始时,您需要让设备驱动程序知道他是从不同的位置开始,以便他可以检索以前的仪器设置。
无论如何,一切顺利(虽然还没有结束......当我的应用程序完成后,我也需要在其他平台上测试它,看看它是否工作相同)。
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).