Playing audio and video files using the Windows Media Player Control.
Access Archon #138
Introduction
Since the earliest days of Access, you have been able to sound a beep from Access code, or a macro (the code is simple: DoCmd.Beep). But if you want something more sophisticated, say playing a piece of music or a video, it wasn’t so easy. But now, with the Windows Media Player control, you can easily play an audio or video file, with only a few lines of code.
Working with the Windows Media Player Control on an Access Form
Although eventually it turned out to be very easy to use this control from VBA code, it wasn’t trivial figuring that out. As is often the case with ActiveX controls placed on Access forms, there is no Help attached to the control, and the Microsoft Knowledge Base and MSDN articles were not very helpful, lacking useful code samples for VBA or even VB (in fact, some help topics labeled as VB Code actually contained C++ code). So in the end I just tried everything I could think of, until finally I found something that worked.
The Windows Media Player control is placed on an Access form using the Insert|ActiveX Control menu (it is way at the bottom of the list). Initially, the control looks like a scaled-down version of the full Windows Media Player. By trial and error, I found two custom properties of this control that I needed to set from VBA code: URL and uiMode (yes, that’s how it is capitalized). Most unintuitively, the URL property takes, not a URL as you would expect, but a standard file path and name, such as “C:My DocumentsMedia FilesSpeech.mp3”. The uiMode property (which lacks a dropdown list for selecting the available choices – again, typical of ActiveX controls on Access forms) takes either “full” (the Windows Media Player interface is visible) or “invisible” (the control is invisible).
I found that if I made the control invisible (to play audio files without an interface), it was invisible in design view as well as form view, and even if I changed the uiMode property back to “full”, the control remained invisible, and I had to delete and reinsert it to have a visible control once more.
Selecting Media Files
I made a form (frmSelectFile, shown in Figure A) with three command buttons in the Detail section, with associated textboxes, for selecting the default media path (this is optional), an audio file, and a video file. The form footer has buttons to play the audio file, play the video file, and close the form.
Figure A. An Access form for selecting and playing audio and video files
The first button, cmdSelectMediaPath, lets you select a folder where you store your media files – if a folder is selected, it will be used as the initial folder for the cmdSelectAudioFile and cmdSelectVideoFile buttons. The button’s Click event procedure uses the Office FileDialog object (introduced in Office XP) to put up a Folder Picker dialog; the selected folder name is stored in the txtMediaPath folder, which is bound to the MediaPath field in tblInfo (I use a table to store the selections on the form, so they will persist from session to session). Note that you have to iterate through the selections in the dialog, even though you are in fact only allowed to select one folder.
VBA Code
Private Sub cmdSelectMediaPath_Click()
On Error GoTo ErrorHandler
Dim strMediaPath As String
‘Create a FileDialog object as a Folder Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
Set txt = Me![txtMediaPath]
With fd
.title = “Browse for Media Path folder”
.ButtonName = “Select”
.InitialView = msoFileDialogViewDetails
If .show = -1 Then
‘Get selected item in the FileDialog SelectedItems collection
‘Have to use collection even if just one item is selected
For Each varSelectedItem In .SelectedItems
Debug.Print varSelectedItem
txt.Value = varSelectedItem
Next varSelectedItem
Else
Debug.Print “User pressed Cancel”
End If
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox “Error No: ” & Err.Number & “; Description: ” & Err.Description
Resume ErrorHandlerExit
End Sub
The code on cmdSelectAudioFile’s Click event first gets the media path, if one has been selected (if none is selected, the dialog will open to the My Documents folder). Then the FileDialog object is used, this time to open a FilePicker dialog, with the InitialView property set to the media path folder variable. FilePicker dialogs can be set to allow multiple choices, but in this case I turned off the AllowMultiSelect property, so that just one file can be selected. As with the FolderPicker dialog, you still have to iterate through the choices, even if there is only one file selected. The selected file is saved in the txtSelectedAudioFile textbox, bound to the AudioFile field in tblInfo.
VBA Code
Private Sub cmdSelectAudioFile_Click()
On Error GoTo ErrorHandler
‘Pick up media path, if one has been selected
strMediaPath = Nz(Me![txtMediaPath].Value)
‘Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set txt = Me![txtSelectedAudioFile]
txt.Value = Null
With fd
.AllowMultiSelect = False
.title = “Browse for File”
.ButtonName = “Select”
.Filters.Clear
.Filters.Add “Audio Files”, “*.mp3; *.wav”, 1
.InitialView = msoFileDialogViewDetails
If strMediaPath <> “” Then
.InitialFileName = strMediaPath
End If
If .show = -1 Then
‘Get selected item in the FileDialog SelectedItems collection
‘Have to use collection even if just one item is selected
For Each varSelectedItem In .SelectedItems
txt.Value = CStr(varSelectedItem)
Next varSelectedItem
Else
Debug.Print “User pressed Cancel”
End If
End With
ErrorHandlerExit:
Set fd = Nothing
Exit Sub
ErrorHandler:
MsgBox “Error No: ” & Err.Number & “; Description: ” & Err.Description
Resume ErrorHandlerExit
End Sub
The code on cmdSelectVideoFile is similar to that on cmdAudioFile.
Supporting Files
The zip file containing this article, in Word format, plus the supporting file(s), may be downloaded from the Access Archon page of my Web site. It is accarch138.zip, which is the last entry in the table of Access Archon columns for Access Watch.
|
Document Name |
Document Type |
Place in |
|
Windows Media Player.mdb |
Access 2002/2003 database |
Wherever you want |
