MCIWnd class
An MCIWnd is a player for sound files, video files and the other media devices of Windows: a window with a play bar and a menu, which shows the picture of a video, and which the user or the program controls.
Dim Path As String = Environ$("TEMP") & "\silence.wav"
WriteSilentWav Path ' a tenth of a second of silence
MCIWnd1.FileName = Path
Debug.Print MCIWnd1.Device; MCIWnd1.Length; " ms, "; MCIWnd1.ModeString
MCIWnd1.Command = "close"
Kill Path
' Output:
' WaveAudio 100 ms, stopped
WriteSilentWav, in the samples of this page, is a procedure of the program’s own that writes a WAV file. The program then gives the control a file, and the user plays it with the play bar.
The control is the MCIWnd window of Windows, the window class MCIWndClass of Video for Windows (msvfw32.dll), which plays a file through the Media Control Interface (MCI). VB6 has no such control; its Multimedia control of MCI32.OCX sends the same MCI commands, but has a row of buttons where this control has a play bar with a track.
Of the common members, the control has those under Name and place, Window handles, VisualStyles of Font and visual styles, Mouse, Keyboard and focus, Dragging the control and OLE drag and drop. It has no font and none of the right-to-left members.
- Opening a file or a device
- Playing and recording
- Positions and time formats
- MCI command strings
- What the user sees
- Properties
- Methods
- Events
- MciCaptionConstants
- MciFormatConstants
- MciModeConstants
- MciNotifyConstants
- See Also
Opening a file or a device
Assigning a path to FileName opens the file with the MCI device that plays files of its type: Device is then WaveAudio for a WAV file, and Mode is MciModeStop. The control raises MediaChange with the name. A file that cannot be opened raises run-time error 53, File not found, after the Error event with the MCI error, and the control then has no file.
ShowOpen lets the user choose the file in the Open dialog instead, and NewDevice opens a device with no file, for a new recording. The user can also open a file from the control’s menu, while AllowOpen is True.
While AutoSizeWindow is True, opening a file gives the control the size of the media’s picture with the play bar under it, or of the play bar alone for a sound file, and raises Resize. With False the control keeps its size.
Dim Path As String = Environ$("TEMP") & "\silence.wav"
WriteSilentWav Path
On Error Resume Next
MCIWnd1.FileName = Environ$("TEMP") & "\no such file.wav"
Debug.Print Err.Number; MCIWnd1.Error
On Error GoTo 0
MCIWnd1.FileName = Path
Debug.Print (MCIWnd1.FileName = Path) & " " & MCIWnd1.CanPlay & " " & MCIWnd1.CanEject
MCIWnd1.Command = "close"
Kill Path
' Output:
' 53 275
' True True False
The device closes when the control is unloaded, and when the program sends it the MCI command close through Command, as the samples of this page do before they delete their file.
Playing and recording
The play bar has a button that plays and stops, a track whose thumb shows and sets the position, and the menu button; with Record a record button too. A program plays with PlayFrom, PlayTo and PlayReverse, and sends the other MCI commands, such as play, pause, stop and record, through Command.
Private Sub cmdPlay_Click()
MCIWnd1.Repeat = False
If MCIWnd1.CanPlay Then MCIWnd1.PlayFrom 0
End Sub
Private Sub cmdPause_Click()
MCIWnd1.Command = "pause"
End Sub
Mode is what the device is doing, and ModeChange is raised when it changes. Position is where the device is, and PositionChange is raised as it moves, checked every TimerFreq milliseconds. Repeat makes the play bar’s button play the media again and again. Volume and Speed are 1000 for the normal level and speed, where the device has them.
Positions and time formats
Position, Length, StartPositon and EndPosition, and the arguments of the play methods, are in the device’s TimeFormat: milliseconds, unless the program sets another format that the device has, such as bytes or samples for a sound file.
Dim Path As String = Environ$("TEMP") & "\silence.wav"
WriteSilentWav Path ' 800 samples of one byte
MCIWnd1.FileName = Path
Debug.Print MCIWnd1.Length; MCIWnd1.EndPosition
MCIWnd1.TimeFormat = MciFormatBytes
Debug.Print MCIWnd1.TimeFormat; MCIWnd1.Length
MCIWnd1.Command = "close"
Kill Path
' Output:
' 100 100
' 8 800
A format the device does not have, such as MciFormatFrames for a sound file, raises the Error event and leaves TimeFormat as it was.
MCI command strings
Command sends an MCI command string to the open device, the string the Windows function mciSendString takes, without the device’s name: the control adds the alias of its device. CommandReturn is what the last command returned, and Error and ErrorString are its MCI error, 0 and the text of success after a command that worked. A command that fails raises the Error event; it raises no run-time error.
Dim Path As String = Environ$("TEMP") & "\silence.wav"
WriteSilentWav Path
MCIWnd1.FileName = Path
MCIWnd1.Command = "status mode"
Debug.Print MCIWnd1.CommandReturn
MCIWnd1.Command = "rewind"
Debug.Print MCIWnd1.Error; MCIWnd1.ErrorString
MCIWnd1.Command = "close"
Kill Path
' Output:
' stopped
' 261 The driver cannot recognize the specified command.
What the user sees
Playbar shows the play bar, Menu its menu button, and Record its record button. Caption gives the control a title bar that shows the file’s name, the position or the mode. For a video, Zoom is the size of the picture as a percentage of its own size, and AutoSizeMovie makes the picture follow the control’s size. ErrorDlg shows the MCI errors to the user in a message box.

Properties
AllowOpen
Whether the control’s menu has the commands that open a file and close it. Boolean. Default: True. It has an effect at run time.
AutoSizeMovie
Whether the picture of a video takes the size of the control when the control is resized. Boolean. Default: True. With False the picture keeps its size.
AutoSizeWindow
Whether the control takes the size of the media when the program opens a file or changes Zoom. Boolean. Default: True. See Opening a file or a device.
BackColor
The color of the control’s window behind the play bar and the picture. OLE_COLOR. Default: vbWindowBackground.
BorderStyle
The border of the control. A member of CCBorderStyleConstants. Default: CCBorderStyleSingle. Another value raises run-time error 380. While Caption is not MciCaptionNone, the control has a title bar and no other border.
Caption
What the title bar of the control shows. A member of MciCaptionConstants. Default: MciCaptionNone, with which the control has no title bar. Another value raises run-time error 380.
Command
An MCI command string, sent to the open device when it is assigned. String. It returns the last command sent. See MCI command strings.
CommandReturn
What the device returned for the last Command. String, read-only: assigning it raises run-time error 383.
Device
The type of the open device, such as WaveAudio for a WAV file. String, read-only: assigning it raises run-time error 383. It is empty while no device is open.
DeviceAlias
The alias by which the control names its device in MCI commands. Long, read-only: assigning it raises run-time error 383.
DeviceID
The MCI device identifier of the open device, for the Windows function mciSendCommand. Long, read-only: assigning it raises run-time error 383. It is 0 while no device is open.
EndPosition
The position of the end of the media, in the TimeFormat. Long, read-only: assigning it raises run-time error 383.
Error
The MCI error number of the last command the control sent to its device. Long, read-only: assigning it raises run-time error 383. It is 0 after a command that worked.
ErrorDlg
Whether the control shows each MCI error to the user in a message box. Boolean. Default: False. The Error event is raised either way.
ErrorString
The text of the MCI error of the last command, such as The driver cannot recognize the specified command. String, read-only: assigning it raises run-time error 383.
FileName
The file the device has open. String. It is empty while no file is open. Assigning a path opens that file; one that cannot be opened raises run-time error 53. See Opening a file or a device.
Length
The length of the media, in the TimeFormat. Long, read-only: assigning it raises run-time error 383.
Menu
Whether the play bar has a menu button, and a right click on the control shows the same menu. Boolean. Default: True. The menu has the commands of the device, such as the volume and the speed and, with AllowOpen, opening and closing a file.
Mode
What the device is doing. A member of MciModeConstants, read-only: assigning it raises run-time error 383. It is MciModeNotReady while no device is open.
ModeString
Mode as the device names it, such as stopped or playing. String, read-only: assigning it raises run-time error 383.
NewDevice
Opens a device of the type assigned, such as waveaudio, with no file, for a new recording that ShowSave can then save. String, write-only: reading it raises run-time error 394.
Playbar
Whether the control shows its play bar. Boolean. Default: True.
Position
The position of the device in the media, in the TimeFormat. Long, read-only: assigning it raises run-time error 383.
PositionString
Position as the device writes it, in its time format: for a device with tracks, the track, minutes, seconds and frames. String, read-only: assigning it raises run-time error 383.
Record
Whether the play bar has a record button and the menu the commands to record. Boolean. Default: False.
Repeat
Whether the play bar’s button plays the media again from the start each time it reaches the end. Boolean. Default: False.
Speed
The speed at which the device plays, 1000 for the normal speed. Long. A device that has no such setting, such as WaveAudio, keeps 1000 and raises the Error event.
StartPositon
The position of the start of the media, in the TimeFormat. Long, read-only: assigning it raises run-time error 383. The name is spelt as the package spells it.
TimeFormat
The unit of the positions and the length. A member of MciFormatConstants. A device starts with MciFormatMilliseconds. A value that is not a member raises run-time error 380; one that the device does not have raises the Error event and leaves the format as it was.
TimerFreq
How often the control checks the device’s mode and position, in milliseconds, and so how often it can raise ModeChange and PositionChange. Integer. Default: 500.
A value of 0 or less raises run-time error 380.
Volume
The volume of the device, 1000 for the normal level. Long. A device that has no such setting, such as WaveAudio, keeps 1000 and raises the Error event.
Zoom
The size of the picture of a video, as a percentage of its own size. Long. Default: 100.
A value of 0 or less raises run-time error 380.
Methods
CanConfig, CanEject, CanPlay, CanRecord, CanSave, CanWindow
Whether the open device can show a dialog box of its settings, eject its media, play, record, save its media to a file, and show a picture in a window. Each returns False while no device is open. For a WAV file CanPlay, CanRecord and CanSave are True and the others False.
Syntax: object.CanPlay As Boolean
Eject
Ejects the device’s media, such as a CD. Returns True when the device did it. A device that cannot eject returns False and raises the Error event.
Syntax: object.Eject As Boolean
PlayFrom
Plays from a position to the end of the media. Returns True when the device started to play.
Syntax: object.PlayFrom ( StartPosition ) As Boolean
- StartPosition
- required A Long: the position to start at, in the TimeFormat.
PlayReverse
Plays backward from the current position to the start of the media, where the device can. Returns True when the device started to play.
Syntax: object.PlayReverse As Boolean
PlayTo
Plays from the current position to a position. Returns True when the device started to play.
Syntax: object.PlayTo ( EndPosition ) As Boolean
- EndPosition
- required A Long: the position to stop at, in the TimeFormat.
Refresh
Draws the control again, as the common Refresh does, and has the device check its media again, so that the play bar shows the current length and position.
Syntax: object.Refresh
ShowOpen
Shows the Open dialog of the package’s CommonDialog class, with the file types that MCI plays, and opens the file the user chooses. Returns True when a file was opened, and False when the user cancelled.
Syntax: object.ShowOpen As Boolean
ShowSave
Shows the Save As dialog of the package’s CommonDialog class, and saves the device’s media, such as a recording, to the file the user chooses. Returns True when the media was saved, and False when the user cancelled.
Syntax: object.ShowSave As Boolean
Events
Error
Raised when a command that the control or the user sent to the device fails.
Syntax: object_Error( ByVal ErrorCode As Long )
- ErrorCode
- The MCI error number, the same as the Error property.
MediaChange
Raised when the device opens a file or a new device, and when it closes.
Syntax: object_MediaChange( ByVal NewFileName As String )
- NewFileName
- The file now open, or the device type for a device with no file. It is empty when the device has closed.
ModeChange
Raised when the device’s Mode changes, such as when it starts or stops playing.
Syntax: object_ModeChange( ByVal NewMode As MciModeConstants )
- NewMode
- The new mode, a member of MciModeConstants.
Notify
Raised when a command sent with the MCI notify flag, such as play notify through Command, has ended.
Syntax: object_Notify( ByVal NotifyCode As MciNotifyConstants )
- NotifyCode
- How the command ended, a member of MciNotifyConstants.
PositionChange
Raised when the device’s Position has changed, at most once each TimerFreq milliseconds.
Syntax: object_PositionChange( ByVal NewPosition As Long )
- NewPosition
- The new position, in the TimeFormat.
Resize
Raised when the size of the control has changed: when the program sets it, and when the control takes the size of the media.
Syntax: object_Resize( )
Signal
Raised when the device reaches a position that an MCI signal command set.
Syntax: object_Signal( ByVal DeviceID As Long, ByVal dwUserParam As Long )
- DeviceID
- The identifier of the device, as DeviceID.
- dwUserParam
- The number that the
signalcommand gave with itsuservaluekeyword.
MciCaptionConstants
The values of Caption.
| Member | Value | Description |
|---|---|---|
| MciCaptionNone | 0 | No title bar. |
| MciCaptionName | 1 | The name of the file. |
| MciCaptionNamePos | 2 | The name and the position. |
| MciCaptionNameMode | 3 | The name and the mode. |
| MciCaptionNamePosMode | 4 | The name, the position and the mode. |
| MciCaptionPos | 5 | The position. |
| MciCaptionPosMode | 6 | The position and the mode. |
| MciCaptionMode | 7 | The mode. |
MciFormatConstants
The values of TimeFormat. Each is the MCI time format of that name, and a device has only some of them.
| Member | Value | Description |
|---|---|---|
| MciFormatMilliseconds | 0 | Milliseconds. |
| MciFormatHms | 1 | Hours, minutes and seconds, a byte each in one number. |
| MciFormatMsf | 2 | Minutes, seconds and frames, a byte each in one number. |
| MciFormatFrames | 3 | Frames of a video. |
| MciFormatSmpte24 | 4 | SMPTE time at 24 frames a second. |
| MciFormatSmpte25 | 5 | SMPTE time at 25 frames a second. |
| MciFormatSmpte30 | 6 | SMPTE time at 30 frames a second. |
| MciFormatSmpte30Drop | 7 | SMPTE time at 30 frames a second, dropping frames. |
| MciFormatBytes | 8 | Bytes of a sound file’s samples. |
| MciFormatSamples | 9 | Samples of a sound file. |
| MciFormatTmsf | 10 | Tracks, minutes, seconds and frames, a byte each in one number. |
MciModeConstants
The values of Mode and of the ModeChange event’s argument.
| Member | Value | Description |
|---|---|---|
| MciModeNotReady | 524 | No device is open, or the device is not ready. |
| MciModeStop | 525 | Stopped. |
| MciModePlay | 526 | Playing. |
| MciModeRecord | 527 | Recording. |
| MciModeSeek | 528 | Moving to a position. |
| MciModePause | 529 | Paused. |
| MciModeOpen | 530 | The device’s door is open, as a CD drive’s. |
MciNotifyConstants
The values of the Notify event’s argument.
| Member | Value | Description |
|---|---|---|
| MciNotifySuccessful | 1 | The command ended as it should. |
| MciNotifySuperseded | 2 | Another command with the notify flag was sent before it ended. |
| MciNotifyAborted | 4 | Another command stopped it. |
| MciNotifyFailure | 8 | The device failed. |
See Also
- Common members – the members this control shares with the package’s other controls
- CommonDialog – the class whose dialogs ShowOpen and ShowSave show