Play a .wma file when opening a form

G

Goirish1904

I have a form that I created that I use everyday. I would like for every
time I open the form for it to play a specific .wma file that I have chosen.
Does anyone know how to do this? Thank you.
 
G

Goirish1904

Doug,

I can't open your database that comes with the Nov 2004 topic. I can open
the PDF file but cannot open the database. It is coming up with an error
saying...the first time you open an earlier version, you must be able to make
changes. I went to uncheck the read only, but I could not because I did not
have access to. Is there anyway you can get me that file to look at?

Thanks.
 
D

Douglas J. Steele

I don't understand what you mean by "I went to uncheck the read only, but I
could not because I did not have access to."

There's no read-only flag on the file (at least, I never set one).

Try creating a new database in whatever version of Access you're using, then
importing everything from the sample.
 
G

Goirish1904

When I go to your November 2004 post about playing music, I save the folder
which holds the PDF file and the .mbd Access database file. When I go to
open the Access database file, it will not let me open it. That is when I
get the error saying that it is an earlier version and I have to be able to
make changes. So, I try to right click on the .mbd file to uncheck a
read-only, but there is nothing to uncheck or to check. It is just shadowed
in and I can do nothing. I would try to use the PDF example, but it is so
scattered that I do not know what to copy and paste into my database. That
is why I was hoping to see that sample in the database that you had created.

If you could just copy and paste the right code into a reply to this, I
would be grateful.

Thank you.
 
D

Douglas J. Steele

Did you try importing into a new database, like I suggested?


'*** Start Code ***

Option Compare Database
Option Explicit

' Extremely simplistic code to use the Media Control Interface APIs to play
an MP3 file
' Samples of how to call the code:
' To play a file:
'
' PlayMP3 "C:\myMP3.mp3"
'
' To stop playing the current file:
'
' StopMP3
'
' To pause the current file:
'
' PauseMP3
'

'The mciGetErrorString function retrieves a string that describes the
specified MCI error code.
Private Declare Function mciGetErrorString Lib "winmm.dll" _
Alias "mciGetErrorStringA" ( _
ByVal dwError As Long, _
ByVal lpstrBuffer As String, _
ByVal uLength As Long _
) As Long

'The mciSendString function sends a command string to an MCI device.
Private Declare Function mciSendString Lib "winmm.dll" _
Alias "mciSendStringA" ( _
ByVal lpstrCommand As String, _
ByVal lpstrReturn128String As String, _
ByVal uReturnLength As Long, _
ByVal hwndCallback As Long _
) As Long

Private Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long _
) As Long

Public Function PlayMP3(FileName As String) As String
' This code was originally written by
' Doug Steele, MVP [email protected]
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: Starts playing a given MP3 file
'
' Inputs: Full path to the MP3 file
'
' Returns: A zero-length string if the file can be played,
' or a string indicating the error if it can't be
'
Dim lngReturn As Long
Dim strCommand As String * 255
Dim strReturn128 As String * 128
Dim strReturn255 As String * 255
Dim strShortPathAndFile As String

' Make sure the file actually exists

If Len(Dir(FileName)) > 0 Then

' MCI doesn't recognize long file names.
' Use GetShortPathName to convert the file name to an
' acceptable format

lngReturn = GetShortPathName(FileName, strReturn255, 255)
strShortPathAndFile = Left$(strReturn255, lngReturn)

' Use mciSendString to indicate what file to play.
' If the file's okay, the call to mciSendString will return 0.
' IN that case, use mciSendString to indicate that playing should begin.
' If it's not okay, a subsequent call to mciGetErrorString
' will indicate what's wrong.
' Note that you must pass an Alias name along with the file when opening it.
' All subsequent calls must use this same alias, and you cannot use the
' same alias again until the close command has been issued for that specific
alias

strCommand = "open " & strShortPathAndFile & " Type MPEGVideo Alias
MyMP3"
lngReturn = mciSendString(strCommand, 0&, 0&, 0&)
If lngReturn = 0 Then ' success
lngReturn = mciSendString("play MyMP3", 0&, 0&, 0&)
If lngReturn = 0 Then ' success
PlayMP3 = vbNullString
Else
Call mciGetErrorString(lngReturn, strReturn128, 128)
PlayMP3 = TrimNull(strReturn128)
Err.Raise vbObjectError + lngReturn, "PlayMP3",
TrimNull(strReturn128)
End If
Else
Call mciGetErrorString(lngReturn, strReturn128, 128)
PlayMP3 = TrimNull(strReturn128)
Err.Raise vbObjectError + lngReturn, "PlayMP3",
TrimNull(strReturn128)
End If
Else
PlayMP3 = "Input File does not exist."
End If

End Function

Public Sub PauseMP3()
' This code was originally written by
' Doug Steele, MVP [email protected]
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: Pauses the currently-playing song
'
' Inputs: None

Dim lngReturn As Long
Dim strReturn128 As String * 128

' Note that you must use the same Alias name as was used in PlayMP3

lngReturn = mciSendString("pause MyMP3", 0&, 0&, 0&)
If lngReturn <> 0 Then ' error
Call mciGetErrorString(lngReturn, strReturn128, 128)
Err.Raise vbObjectError + lngReturn, "PauseMP3",
TrimNull(strReturn128)
End If

End Sub

Public Sub ResumeMP3()
' This code was originally written by
' Doug Steele, MVP [email protected]
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: Resumes playing the currently loaded file
'
' Inputs: None

Dim lngReturn As Long
Dim strReturn128 As String * 128

' Note that you must use the same Alias name as was used in PlayMP3

lngReturn = mciSendString("resume MyMP3", 0&, 0&, 0&)
If lngReturn <> 0 Then ' error
Call mciGetErrorString(lngReturn, strReturn128, 128)
Err.Raise vbObjectError + lngReturn, "ResumeMP3",
TrimNull(strReturn128)
End If

End Sub

Public Sub StopMP3()
' This code was originally written by
' Doug Steele, MVP [email protected]
' You are free to use it in any application
' provided the copyright notice is left unchanged.
'
' Description: Stops playing the current song
'
' Inputs: None

' Note that you must use the same Alias name as was used in PlayMP3

Dim lngReturn As Long
Dim strReturn128 As String * 128

lngReturn = mciSendString("stop MyMP3", 0&, 0&, 0&)
If lngReturn = 0 Then ' success
lngReturn = mciSendString("close MyMP3", 0&, 0&, 0&)
If lngReturn <> 0 Then ' error
Call mciGetErrorString(lngReturn, strReturn128, 128)
Err.Raise vbObjectError + lngReturn, "StopMP3",
TrimNull(strReturn128)
End If
Else
' If the device has already been closed, lngReturn will be 263
' ("The specified device is not open or is not recognized by MCI.")
' Ignore that error, since it'll usually mean that we've already
' closed the device by clicking on the Stop button.
If lngReturn <> 263 Then
Call mciGetErrorString(lngReturn, strReturn128, 128)
Err.Raise vbObjectError + lngReturn, "StopMP3",
TrimNull(strReturn128)
End If
End If

End Sub

' *** End Code ***

It's possble that you might have to use something other than:

strCommand = "open " & strShortPathAndFile & " Type MPEGVideo Alias MyMP3"

The "Alias MyMP3" will be fine, but the "MPEGVideo" may need to be changed.
 
G

Goirish1904

That worked when I imported the database into a new one..so, I was able to
see the code and everything. The MP3 form does work and plays the song just
fine. One more question for you though...Do you know how to make it to where
when I open up the form, it will automatically play a defaulted song I have
chosen without even having to click on start?

Thanks Doug.
 
D

Douglas J. Steele

Put the code that's in the Click event for the Start button into the form's
Load event.
 
Top