Results 1 to 6 of 6

Thread: how do you play mids in the background of a form?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Northampton, UK
    Posts
    3

    how do you play mids in the background of a form?

    could someone please gove me an example code of playing a mid, (filenamed finallevel_backmusic.mid) in the background of a form - also can u loop it?

    thanks


    FLY

  2. #2
    Addicted Member Stephenb's Avatar
    Join Date
    Nov 2000
    Location
    Phoenix, Arizona.
    Posts
    172
    Try this.
    Link
    Stephen Boston
    Onward!
    VB6 Pro SP4, VBScript
    A+ Certified Techncian.
    No matter where you go, there you are.

  3. #3
    Addicted Member Stephenb's Avatar
    Join Date
    Nov 2000
    Location
    Phoenix, Arizona.
    Posts
    172
    That link did not answer your looping question. Sorry

    Try this one instead.

    They are mainly talking about mp3's but use midi instead.

    Hope that helps better.
    Stephen Boston
    Onward!
    VB6 Pro SP4, VBScript
    A+ Certified Techncian.
    No matter where you go, there you are.

  4. #4
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    I see that you've already gotten how to do this, but on the looping thing, I've tried all sorts of things, but none have worked. I thought perhaps the notify flag could be used, and work with the hWndCallback parameter, but that is a window handle, so it doesn't make sense to me. I could never get a concrete description on how to work with that. But there are ways to tell if a media file is still playing though. This is some quick and dirty stuff from a module I have when I don't want to type out the stuff myself. The important part is the last function, IsFinished(), which tells if a given MCI alias is complete. It uses the time format called "song pointer" (which I think is the default for midi files), so it might be better off to either use the OpenMIDI() sub here, or adapt yours to force the time format song pointer as in the OpenMIDI() sub here. To make a midi loop, all you'd have to do is set a timer or something, which is somewhat of a bad way around it, and then use this function to tell when the file is over. It's not the best of ways around it, but it works. I'd still prefer a way to instantly know when it's over, and loop it, without constantly checking.

    VB Code:
    1. Option Explicit
    2.  
    3. Public Declare Function mciSendString Lib "winmm" Alias "mciSendStringA" (ByVal lpzCommand As String, ByVal lpzReturn As String, ByVal nReturnLength As Long, ByVal hWndCallback As Long) As Long
    4.  
    5. Public Sub OpenMIDI(ByVal strMIDI As String, ByVal strAlias As String)
    6.    'opens the midi specified in strMIDI with the alias strAlias
    7.    mciSendString "open " & strMIDI & " type sequencer alias " & strAlias, vbNullString, 0, 0
    8.    'makes the time format "song pointer" (used to tell if the file is done playing later)
    9.    mciSendString "set " & strAlias & " time format song pointer", vbNullString, 0, 0
    10. End Sub
    11.  
    12. Public Sub PlayMIDI(ByVal strAlias As String)
    13.    'this function assumes strAlias is an aliased MCI device
    14.    'plays the midi aliased as strAlias
    15.    mciSendString "play " & strAlias & " from 0", vbNullString, 0, 0
    16. End Sub
    17.  
    18. Public Sub StopMIDI(ByVal strAlias As String)
    19.    'this function assumes strAlias is an aliased MCI device
    20.    'stop the midi aliased as strAlias
    21.    mciSendString "stop " & strAlias, vbNullString, 0, 0
    22. End Sub
    23.  
    24. Public Sub CloseMIDI(Optional ByVal strAlias As String = "")
    25.    'closes the midi aliased as strAlias, or all open MCI devices if no alias is given, or "" is given
    26.    mciSendString "close " & IIf(strAlias = "", "all", strAlias), vbNullString, 0, 0
    27. End Sub
    28.  
    29. Public Function IsFinished(ByVal strAlias As String) As Boolean
    30.    'this function assumes strAlias is an aliased MCI device
    31.    'and that the aliased device has the time format "song pointer"
    32.    'if the given alias isn't an open MCI device, True is returned
    33.    Dim strBuffer As String * 128
    34.    Dim lngLength As Long, lngPosition As Long
    35.    'get the current length of the midi aliased as strAlias (in song pointer units)
    36.    mciSendString "status " & strAlias & " length", strBuffer, Len(strBuffer), 0
    37.    'trim out the valid part of the string, and store in the var
    38.    lngLength = CLng(Val(Left$(strBuffer, InStr(strBuffer, vbNullChar) - 1)))
    39.    'get the current position midi aliased as strAlias (in song pointer units)
    40.    mciSendString "status " & strAlias & " position", strBuffer, Len(strBuffer), 0
    41.    'trim out the valid part of the string, and store in the var
    42.    lngPosition = CLng(Val(Left$(strBuffer, InStr(strBuffer, vbNullChar) - 1)))
    43.    'if the current position is equal to the length, it's over
    44.    IsFinished = IIf(lngPosition = lngLength, True, False)
    45. End Function
    46.  
    47. 'open a midi
    48. OpenMIDI "c:\myfile.mid", "myfile"
    49.  
    50. 'play the midi (same sytax for StopMIDI and CloseMIDI)
    51. PlayMIDI "myfile"
    52.  
    53. 'check on status
    54. If IsFinished("myfile") Then MsgBox "myfile is done playing"
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    I have some good news. After giving this another go, I finally had the insight to understand why the last param is called hWndCallback. WIth the notify flag, the MCI device will send a MM_MCINOTIFY message back to the window you specify when the device is finished what it was doing. To catch this, you'll have to subclass, so it's not necessarily the safest thing to do (but is fine as long as you do it correctly). This is what you'll need to set it up. Since this is subclassing, DO NOT press the IDE stop button, or you'll crash VB. Press the X on the form, or use Alt+F4. This isn't the fanciest example, as it only uses a single playing midi, but it shows how to get the loop effect.
    VB Code:
    1. 'in a module
    2. Option Explicit
    3.  
    4. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    5. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    6.  
    7. Public Const GWL_WNDPROC As Long = -4
    8. Public Const MCI_NOTIFY_SUCCESSFUL As Long = &H1
    9. Public Const MCI_NOTIFY_ABORTED As Long = &H4
    10. Public Const MM_MCINOTIFY As Long = &H3B9
    11.  
    12. Public lngOldProc As Long
    13.  
    14. Public Sub Hook(ByVal hWnd As Long)
    15.    lngOldProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf CustomProc)
    16. End Sub
    17.  
    18. Public Sub UnHook(ByVal hWnd As Long)
    19.    SetWindowLong hWnd, GWL_WNDPROC, lngOldProc
    20. End Sub
    21.  
    22. Public Function CustomProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    23.    If wMsg = MM_MCINOTIFY Then
    24.       'if the previous command finished successfully, replay the midi
    25.       'this assumes the midi has been aliased as "x"
    26.       If wParam And MCI_NOTIFY_SUCCESSFUL Then mciSendString "play x from 0 notify", vbNullString, 0, hWnd
    27.       'the following isn't necessary, but it's something additional you can check
    28.       'if you use mciSendString and "play", "stop", or "close" on the midi
    29.       'while playing, MCI will set this flag
    30.       If wParam And MCI_NOTIFY_ABORTED Then Debug.Print "aborted"
    31.    End If
    32.    'let windows handle the rest
    33.    CustomProc = CallWindowProc(lngOldProc, hWnd, wMsg, wParam, lParam)
    34. End Function
    35.  
    36.  
    37. 'inside your form
    38. Private Sub Form_Load()
    39.    Hook Me.hWnd
    40.    mciSendString "open c:\x.mid type sequencer alias x", vbNullString, 0, 0
    41.    mciSendString "play x from 0 notify", vbNullString, 0, Me.hWnd
    42. End Sub
    43.  
    44. Private Sub Form_Unload(Cancel As Integer)
    45.    mciSendString "close x", vbNullString, 0, 0
    46.    UnHook Me.hWnd
    47. End Sub
    Last edited by Kaverin; Aug 9th, 2001 at 11:57 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  6. #6
    Addicted Member Stephenb's Avatar
    Join Date
    Nov 2000
    Location
    Phoenix, Arizona.
    Posts
    172
    Kaverin-

    That looks great! If you don't mid I'll add that to my code lib.
    Stephen Boston
    Onward!
    VB6 Pro SP4, VBScript
    A+ Certified Techncian.
    No matter where you go, there you are.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width