Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Animation Without Timer Control

0.00/5 (No votes)
2 Feb 2008 2  
Using Multimedia MCI Control to make animation in VB6

AudioPlayer

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 slow, 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 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 other variables in the declarations part.

LoadMusicFile() procedure:

'
' Set the number of milliseconds =0, let StatusUpdate events not occurred.:
'
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:

' ' 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 occurred:
'
mciWave.UpdateInterval = 0
'
' Pauses playing using the MCI_PAUSE command:
'
mciWave.Command = "Pause"

btnStop_Click() procedure:

'
' StatusUpdate events not occurred:
'
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 the complete code. For more help, you can refer to Multimedia MCI Control at Visual Basic Help.

Remarks

When extracting the AudioPlayer.zip file, you can find the 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 ideas or if you find any problems. Thanks to Code Project and thanks to all.

History

  • 2nd February, 2008: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here