Introduction
In this article I am using (Multimedia MCI) Control to make animation as (Timer) Control. You can use (StatusUpdate) Event to do this. The (StatusUpdate) Event occurs automatically at intervals which you give to the (UpdateInterval) property suitable value > 0 exactly as (Timer) Control Interval.
Background
With (Multimedia MCI) Control You must know that The (StatusUpdate) Event allows an application to update the display to inform the user about the status of the current MCI device as Position, Length, and Mode. You can give the (UpdateInterval) property any positive value (in milliseconds). If this value is small the display is fast, if value is large the display is slowly, but if the value is 0 no StatusUpdate events occur.
My form has some controls:
mciWave (MMControl), not Visible.
ImageList1 includes 30 images.
ImageList2 includes 6 images.
DancePic (Picture box) to hold images which ImageList2 has included.
MoviePic (Picture box) to hold images which ImageList1 has included.
btnPlay (Button ) to execute the (Play) command.
btnPause (Button ) to execute the (Pause) command.
btnStop (Button ) to execute the (Stop) command.
btnOpen (Button ) to load the music file.
SongName (Label) for file name.
SongTime (Label) for time length of file.
ElapsedTime (Label) for time Elapsed time.
CommonDialog1 (CommonDialog) to load music files in format (mid, mp3, wav, wma).
Using the Code
Give (UpdateInterval) property the value 50 as constant in the declarations. Refer to another variables in declarations part.
LoadMusicFile () procedure :
'
' Set the number of milliseconds =0, let StatusUpdate events not occured.:
'
mciWave.UpdateInterval = 0
'
' Display the File Open dialog box:
'
With CommonDialog1
.FilterIndex = 1
.Flags = cdlOFNReadOnly Or cdlOFNFileMustExist
.CancelError = True
.FileName = ""
.Filter = "Sound Formats|*.mid;*.mp3;*.wav;*.wma|mid (*.mid)|*.mid|mp3 (*.mp3)|*.mp3|wav (*.wav)|*.wav|wma (*.wma)|*.wma"
.DialogTitle = "Choose sound file..."
End With
CommonDialog1.ShowOpen
If Err <> 0 Then ' No file selected to play.
Exit Sub
End If
MusicFile = CommonDialog1.FileName
MusicName = CommonDialog1.FileTitle
InitPlay ' Go to InitPlay() procedure
InitPlay() procedure :
<pre style="FONT-SIZE: 9pt; BACKGROUND: #fbedbb; MARGIN: 0in 0in 0pt; FONT-FAMILY: Courier New">'
' Close a device if open:
'
If Not mciWave.Mode = mciModeNotOpen Then
mciWave.Command = "Close"
End If
'
' Opens a device using the MCI_OPEN command:
'
mciWave.Command = "Open"
' Time of the song:
mciWave.TimeFormat = mciFormatMilliseconds
msec = (CDbl(mciWave.Length) / 1000)
btnPlay_Click () procedure:
'
' Set the number of milliseconds = ConInterval, let StatusUpdate events:
'
mciWave.UpdateInterval = ConInterval
'
' Plays a device using the MCI_PLAY command:
'
mciWave.Command = "Play"
btnPause_Click () procedure :
'
' StatusUpdate events not occured:
'
mciWave.UpdateInterval = 0
'
' Pauses playing using the MCI_PAUSE command:
'
mciWave.Command = "Pause"
btnStop_Click () procedure:
'
' StatusUpdate events not occured:
'
mciWave.UpdateInterval = 0
'
' Stops playing using the MCI_STOP command:
'
mciWave.Command = " Stop"
mciWave_StatusUpdate () procedure:
'
' If the device is not playing, reset to the beginning:
'
If mciWave.Position = mciWave.Length Then
mciWave.UpdateInterval = 0 ' no StatusUpdate events occur.
Exit Sub
End If
'
' Determine how much of the file has played:
'
CurrentValue = mciWave.Position
Value = CDbl((CurrentValue / 1000))
'
' view elapsed time:
'
ElapsedTime.Caption = Format$(Value, "00:00")
'
' Wait a moment before change new picture from ImageList2:
'
Counter = Counter + 1: If Counter = 25 Then Counter = 0
' view dance picture from ImageList2:
If (Counter / 5) = Int(Counter / 5) Then
PicNum = Counter / 5 + 1
DancePic.Picture = ImageList2.ListImages(PicNum).Picture
End If
'
' Wait a moment before change new picture from ImageList1:
'
MasterCounter = MasterCounter + 1: If MasterCounter = 1500 Then MasterCounter = 0
' view dance picture from ImageList1:
If (MasterCounter / 50) = Int(MasterCounter / 50) Then
ImgNum = MasterCounter / 50 + 1
MoviePic.Picture = ImageList1.ListImages(ImgNum).Picture ' view new picture
End If
You can go back to source files of the project (AudioPlayer) to read complete code. For more help you can refer to (Multimedia MCI Control) at Visual Basic Help.
Remarks
When extract the file AudioPlayer.zip you can find source files of the project AudioPlayer
Last Words
I hope this article is useful and helps you to create some applications using (MMControl) as (Timer). Please tell me if you have any idea or if you find any problems. Thanks for Code Project and thanks for all.
Mostafa Kaisoun