Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePlay and stop a MIDI file by using the MCI control
DescriptionThis example shows how to play a MIDI file continuously by using the MCI control in Visual Basic 6.
KeywordsMIDI, audio, MCI, multimedia
CategoriesMultimedia
 
When the program starts, it initializes the MCI control.

When the user clicks the Play/Stop button, the determines whether it is playing a file. If the program is playing a file, it sets the control's Command to Stop to stop playing. If the program isn't playing a file, it sets the control's FileName property to the name of the MIDI file, sets Wait to True so the control opens the file synchronously, and sets Command = Open to open the file. It then sets Command = Play to play the file. It does not set Wait = True first so the play is asynchronous.

When the control finishes playing, the program closes the file. If the Play button still reads "Stop," the program restarts the file.

 
Private Sub Form_Load()
    txtFilename.Text = App.Path
    If Right$(txtFilename.Text, 1) <> "\" _
        Then txtFilename.Text = txtFilename.Text & "\"
    txtFilename.Text = txtFilename.Text & "Passport.mid"

    ' Prepare the MCI control for WaveAudio.
    MMControl1.Notify = False
    MMControl1.Wait = True
    MMControl1.Shareable = False
    MMControl1.DeviceType = "Sequencer"
End Sub

' Open the device and play the sound.
Private Sub cmdStart_Click()
    ' See if we are playing.
    If cmdStart.Caption = "Stop" Then
        ' We are playing. Stop.
        cmdStart.Caption = "Start"

        MMControl1.Command = "Stop"
    Else
        ' We are not playing. Start.
        cmdStart.Caption = "Stop"

        ' Set the file name.
        MMControl1.FileName = txtFilename.Text

        ' Open the MCI device.
        MMControl1.Wait = True
        MMControl1.Command = "Open"

        ' Play the sound without waiting.
        MMControl1.Command = "Play"
    End If
End Sub

Private Sub MMControl1_Done(NotifyCode As Integer)
    ' Close the device.
    MMControl1.Command = "Close"

    ' See if we should restart.
    If Me.cmdStart.Caption = "Stop" Then
        ' Set the file name.
        MMControl1.FileName = txtFilename.Text

        ' Open the MCI device.
        MMControl1.Wait = True
        MMControl1.Command = "Open"

        ' Play the sound without waiting.
        MMControl1.Command = "Play"
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated