Looks at playing media files in Windows Media Player.
Access Archon #138
Playing Media Files
After (optionally) selecting a Media Path, and selecting an audio or video file, you can use the cmdPlayAudioFile or cmdPlayVideoFile buttons in the form footer to play the selected file. To illustrate two different ways of using the Windows Media Player control, I chose to play audio files with no player interface, using an invisible control on frmSelectFile, but video files are played in a visible control on a separate pop-up form.
For audio files, the code first picks up the name of the selected file from txtSelectedAudioFile, and then uses the FileExists function to check that the file name and path is valid, using components of the FileSystemObject object. If the file name is valid, then frmWindowsMediaPlayer is closed (if it is open – in case it was previously opened to play a video file), the frmSelectFile form is moved to its default position, centered, and the Windows Media Player control’s URL property is set to the audio file name; at this point the file starts playing automatically.
VBA Code
Private Sub cmdPlayAudioFile_Click()
On Error GoTo ErrorHandler
strAudioFile = Nz(Me![txtSelectedAudioFile].Value)
If strAudioFile = “” Then
strTitle = “No file selected”
strPrompt = “Please select an audio file to play”
DoCmd.Beep
MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
Me![txtSelectedAudioFile].SetFocus
GoTo ErrorHandlerExit
Else
‘Check that file name and path is valid
Debug.Print “Selected audio file: ” & strAudioFile
If FileExists(strAudioFile) = False Then
strTitle = “Invalid file name”
strPrompt = “File name and/or path is invalid; please re-select file”
DoCmd.Beep
MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
Me![txtSelectedAudioFile].SetFocus
GoTo ErrorHandlerExit
Else
Set prj = Application.CurrentProject
If prj.AllForms(“frmWindowsMediaPlayer”).IsLoaded Then
DoCmd.Close acForm, “frmWindowsMediaPlayer”
End If
Me.Move Left:=2500, Top:=2500
With Me![ocxWindowsMediaPlayer]
.URL = strAudioFile
End With
End If
End If
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox “Error No: ” & Err.Number & “; Description: ” & Err.Description
Resume ErrorHandlerExit
End Sub
Private Function FileExists(strFile As String) As Boolean
On Error Resume Next
Dim fso As New Scripting.FileSystemObject
Dim fil As Scripting.File
Set fil = fso.GetFile(strFile)
If fil Is Nothing Then
FileExists = False
Else
FileExists = True
End If
End Function
The procedure is a little different for video files. After initial checking of file name and path validity, similar to the code for playing audio files, the code then opens frmWindowsMediaPlayer, and adjust the positions of that form and frmSelectFile, so that frmWindowsMediaPlayer is located over frmSelectFile. Then the code sets the URL property of the Windows Media Player control on frmWindowsMediaPlayer to the video file name, and the video starts playing automatically, as shown in Figure B.
Figure B. Playing a video file in the Windows Media Player control
Note: You’ll have to imagine the video – my screen capture program didn’t capture it
VBA Code
Private Sub cmdPlayVideoFile_Click()
On Error GoTo ErrorHandler
strVideoFile = Nz(Me![txtSelectedVideoFile].Value)
If strVideoFile = “” Then
strTitle = “No file selected”
strPrompt = “Please select a video file to play”
DoCmd.Beep
MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
Me![txtSelectedVideoFile].SetFocus
GoTo ErrorHandlerExit
Else
‘Check that file name and path is valid
Debug.Print “Selected video file: ” & strVideoFile
If FileExists(strVideoFile) = False Then
strTitle = “Invalid file name”
strPrompt = “File name and/or path is invalid; please re-select file”
DoCmd.Beep
MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
Me![txtSelectedVideoFile].SetFocus
GoTo ErrorHandlerExit
Else
Set prj = Application.CurrentProject
If prj.AllForms(“frmWindowsMediaPlayer”).IsLoaded Then
Forms![frmWindowsMediaPlayer].Visible = True
Else
DoCmd.OpenForm “frmWindowsMediaPlayer”
End If
Me.Move Left:=2500, Top:=5450
Set frmWMP = Forms![frmWindowsMediaPlayer]
frmWMP.Move Left:=2500, Top:=100
With frmWMP![ocxWindowsMediaPlayer]
.SetFocus
.URL = strVideoFile
End With
End If
End If
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox “Error No: ” & Err.Number & “; Description: ” & Err.Description
Resume ErrorHandlerExit
End Sub
References
The code in the sample database needs the following references (in addition to the default references):
Microsoft DAO 3.6 Object Library
Microsoft Scripting Runtime
Microsoft Office 11.0 Object Library (for Office XP and later, this library supports the FileDialog object)
Windows Media Player (this reference is added automatically when you place the Windows Media Player control on a form)
If you import code or objects into a database of your own, you may need to set one or more of these references. The version number may differ, depending on your Office version; check the version you have. References are set in the References dialog, opened from the VBA window. For more information on working with references, see Access Archon #107, Working with References.
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 |