【问题标题】:MIDI sound in VB.NET: after pausing & resuming, it sounds differentVB.NET中的MIDI声音:暂停和恢复后,听起来不同
【发布时间】:2011-11-20 21:29:21
【问题描述】:

我正在尝试在我的 VB.NET (VB 2010 Express) 中播放一个 MIDI 文件,我将 this other question here on 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 上

非常感谢!最好的

【问题讨论】:

标签: vb.net midi windows-7-x64


【解决方案1】:

好的,我想出了怎么做。这是代码,大致灵感来自我在上面评论中提到的 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

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

不管怎样,一切都很好(虽然还没有结束……当我的应用程序完成后,我还需要在其他平台上对其进行测试,看看它是否可以正常工作)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多