Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511

    Play MIDI for me

    My challenge is to be able to play MIDI files by clicking on an Excel version 2003 cell, in a list that contains many rows of MIDI file names (for each name there is a corresponding MIDI file). I can do this with the VBA code below which I found on the web and modified. A command button allows the playing to be stopped, otherwise after a minute or two, a file will reach the end and stop playing.

    How can I prevent an attempt to play a second MIDI file while one file is playing? I am looking for a way to know that the file is either still playing or finished. When it is still playing, my stopping code works. If I try to run the stopping code when no file is playing, I get the usual error message.

    I checked task manager processes while playing a MIDI file to see if a process gets added to the list, and it does not, so I can't test for a running process. Any ideas out there?
    Jerry

    Code:
    Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
    Public isMIDIPlaying As Boolean
    
    Sub Play_MIDI(strMidiFile As String)
    
        If isMIDIPlaying Then Exit Sub
        
        If Application.CanPlaySounds Then
            strMidiFile = ThisWorkbook.Path & Application.PathSeparator & strMidiFile
            If (VBA.Dir(strMidiFile) <> "") Then
                'The double quotes Chr$(34) are needed when the file path contains spaces.
                'First OPEN and then PLAY.
                mciExecute ("OPEN " & VBA.Chr$(34) & strMidiFile & VBA.Chr$(34) & " ALIAS jingle")
                mciExecute ("PLAY jingle")
                 
                isMIDIPlaying = True
             
             End If
          End If
      End Sub
    
    Sub Stop_MIDI(strMidiFile As String)
    
        If Application.CanPlaySounds Then
            strMidiFile = ThisWorkbook.Path & Application.PathSeparator & strMidiFile
            If (VBA.Dir(strMidiFile) <> "") And isMIDIPlaying Then
                mciExecute ("CLOSE jingle")
            End If
        End If
        
        isMIDIPlaying = False
        
        Exit Sub
    
    End Sub

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Use a lock file, call it "IsMIDIPlaying" and create it, delete it, and test for its existence at the appropriate places.
    Don't forget to attempt to delete the file on system start up.
    I usually use the process-id as the contents of the file.

  3. #3
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511
    I don't understand what your solution is. Could you expand on it a little.
    What does "appropriate places" mean? What would trigger checking for the existence of the created file and what would trigger its deletion?

    If you have done this before with MIDI files, how did you identify the running process? I compared about 65 process names before playing the MIDI and during the playing, using the code below; there is no difference. I conclude that the code to play MIDI files that I am using does not start a new process. Am I wrong? Thanks.

    Code:
    Private Sub cmdProcess_Click()
    'store names of processes currently running
    Dim sProcessName As String, sComputer As String
    Dim j As Integer
    
        j = 1
        sComputer = "."
        Set oWmi = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colProcessList = oWmi.ExecQuery("Select * from Win32_Process")
        
        For Each oProcess In colProcessList
            Worksheets("Sheet5").Cells(j, 9).Value = oProcess.Name
            j = j + 1
        Next
        
        MsgBox "Done with listing Processes in Sheet5."
    
    End Sub

  4. #4
    Join Date
    Jan 2002
    Location
    Bay Area
    Posts
    511
    I have found a solution to controlling MIDI file playing from the Excel worksheet.

    A Stop MIDI command button is initially disabled and becomes enabled from the code that starts the MIDI file playing. Clicking the enabled Stop button disables it. This button's properties were set to not take the focus.

    A list of MIDI file names is displayed. When a cell in column E contains a "P" (which means Play), the MIDI file begins to play when the column E cell is clicked, and every "P" (which is every cell in the column starting at row #3) is turned from black to light gray. At the same time, the "Stop MIDI" command button is enabled. Column E font will not be changed to black until the Stop MIDI button is pressed. If the font in E3 is light gray, the VBA subroutine to play a MIDI file exits and does not play the file.

    In summary, once a MIDI file is playing or even finished, further MIDI file playing is disabled until the Stop MIDI button is pressed.

    Playing MIDIs is an afterthought for this application. It is a compilation of sheet music books in dozens of PDF files that are available on the internet, including lots of Fake books.
    Adobe Reader is called from VBA code that also provides Adobe the page number and PDF file name to open. From there, the page or pages containing the song can be printed if the song is a "keeper". Some of the songs will have MIDI files. The list now exceeds 10,000 rows and file names and page numbers were generated by scanning the tables of contents with OCR software, then greatly edited.

    If anyone has insights into the original issue, being able to test whether a MIDI file has stopped playing, I am curious about this even though I have a solution that seems to be user-friendly.

    RESOLVED
    Last edited by JerryDal; 03-18-11 at 02:21. Reason: clarification, wording

Posting Permissions

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