VB.NET 中的 MIDI 声音:暂停后&继续,听起来不一样

发布于 2024-12-17 03:48:31 字数 2611 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2024-12-24 03:48:31

好的,我知道该怎么做了。这是代码,大致受我在上面的评论中提到的 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

我猜,当从头开始播放歌曲时,播放器会读取仪器信息。当它从不同的位置开始时,您需要让设备驱动程序知道他是从不同的位置开始,以便他可以检索以前的仪器设置。

无论如何,一切顺利(虽然还没有结束......当我的应用程序完成后,我也需要在其他平台上测试它,看看它是否工作相同)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文