2011-11-19 68 views
1

我想在我的VB.NET(VB 2010 Express)中播放一个MIDI文件,并且与我翻译的代码this other question here on Stack Overflow从C到VB。VB.NET中的MIDI声音:暂停和恢复后,它听起来不同

但是,我也需要暂停,而该代码仅用于打开和停止。我编辑这样的代码:

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

显然,有人有同样的问题,并能解决它: [http://www.freebasic.net/forum/viewtopic.php?p=23358&sid=937c1f6c627fec492be6eb08f837fa55][1] 我会明天检查一下。 谢谢大家! [1]:http://www.freebasic.net/forum/viewtopic.php?p=23358&sid=937c1f6c627fec492be6eb08f837fa55 – Davide

回答

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 

我猜即从一开始播放歌曲时,播放器读取乐器信息。当它从一个不同的位置开始时,您需要让设备驱动程序知道他从一个不同的位置开始,以便他可以检索先前的仪器设置。无论如何,一切都很好(尽管它还没有结束......当我的应用程序完成后,我还需要在其他平台上测试它,看看它是否工作相同)。