Having trouble playing MP3 files, although no issue playing a .WAV.

Here is my module code:

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Private Const SND_APPLICATION = &H80 ' look for application
specific association
Private Const SND_ALIAS = &H10000 ' name is a WIN.INI
[sounds] entry
Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI
[sounds] entry identifier
Private Const SND_ASYNC = &H1 ' play asynchronously
Private Const SND_FILENAME = &H20000 ' name is a file name
Private Const SND_LOOP = &H8 ' loop the sound until next
sndPlaySound
Private Const SND_MEMORY = &H4 ' lpszSoundName points to a
memory file
Private Const SND_NODEFAULT = &H2 ' silence not default, if
sound not found
Private Const SND_NOSTOP = &H10 ' don't stop any currently
playing sound
Private Const SND_NOWAIT = &H2000 ' don't wait if the driver
is busy
Private Const SND_PURGE = &H40 ' purge non-static events
for task
Private Const SND_RESOURCE = &H40004 ' name is a resource name
or atom
Private Const SND_SYNC = &H0 ' play synchronously
(default)

Public gSoundsOn As Integer

Public Sub PlayMP3(SoundFileName As String)

sndPlaySound SoundFileName, SND_ASYNC Or SND_NODEFAULT

End Sub

.. to play a sound, I simply do this

PlayMP3 "filename.mp3"

Well, if I pass a .wav file, it plays. If I pass an mp3, it does not.

Any help is appreciated. Thanks -

Re: Any Way to Play an MP3 by Kevin

Kevin
Wed Oct 15 21:29:22 CDT 2008

It works if the appropriate drivers are installed.

"OneNerd" <OneNerd@discussions.microsoft.com> wrote in message
news:A864AF6C-AC57-460D-B7D4-59AF01C0DBD0@microsoft.com...
| Having trouble playing MP3 files, although no issue playing a .WAV.
|
| Here is my module code:
|
| Private Declare Function sndPlaySound Lib "winmm.dll" Alias
"sndPlaySoundA"
| ( _
| ByVal lpszSoundName As String, _
| ByVal uFlags As Long) As Long
|
| Private Const SND_APPLICATION = &H80 ' look for application
| specific association
| Private Const SND_ALIAS = &H10000 ' name is a WIN.INI
| [sounds] entry
| Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI
| [sounds] entry identifier
| Private Const SND_ASYNC = &H1 ' play asynchronously
| Private Const SND_FILENAME = &H20000 ' name is a file name
| Private Const SND_LOOP = &H8 ' loop the sound until
next
| sndPlaySound
| Private Const SND_MEMORY = &H4 ' lpszSoundName points to
a
| memory file
| Private Const SND_NODEFAULT = &H2 ' silence not default, if
| sound not found
| Private Const SND_NOSTOP = &H10 ' don't stop any
currently
| playing sound
| Private Const SND_NOWAIT = &H2000 ' don't wait if the
driver
| is busy
| Private Const SND_PURGE = &H40 ' purge non-static events
| for task
| Private Const SND_RESOURCE = &H40004 ' name is a resource name
| or atom
| Private Const SND_SYNC = &H0 ' play synchronously
| (default)
|
| Public gSoundsOn As Integer
|
| Public Sub PlayMP3(SoundFileName As String)
|
| sndPlaySound SoundFileName, SND_ASYNC Or SND_NODEFAULT
|
| End Sub
|
| .. to play a sound, I simply do this
|
| PlayMP3 "filename.mp3"
|
| Well, if I pass a .wav file, it plays. If I pass an mp3, it does not.
|
| Any help is appreciated. Thanks -



Re: Any Way to Play an MP3 by Larry

Larry
Thu Oct 16 07:02:51 CDT 2008


"OneNerd" <OneNerd@discussions.microsoft.com> wrote

<...>

> Well, if I pass a .wav file, it plays. If I pass an mp3, it does not.
>
> Any help is appreciated. Thanks -

Here is another method:

LFS




Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As _
String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long


Private Sub Command1_Click()
Dim song As String
If Len(Command1.Tag) Then
' Stop playing
mciSendString "Close All", 0, 0, 0
Command1.Caption = "Play"
Command1.Tag = ""
Else
' Play selected song
song = "... your song ..." ' Full path to an MP3 file
mciSendString "Close mp3", 0, 0, 0
mciSendString "Open """ & song & """ Alias mp3", 0, 0, 0
mciSendString "Play mp3", 0, 0, 0
Command1.Caption = "Stop"
Command1.Tag = "playing"
End If
End Sub



Re: Any Way to Play an MP3 by Alfie

Alfie
Thu Oct 16 07:15:21 CDT 2008

On Wed, 15 Oct 2008 19:20:05 -0700, OneNerd
<OneNerd@discussions.microsoft.com> wrote:

>Having trouble playing MP3 files, although no issue playing a .WAV.
>
>Here is my module code:
>
>Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
>( _
> ByVal lpszSoundName As String, _
> ByVal uFlags As Long) As Long

sndPlaySound cannot handle MP3s, you'll need to either use the media
player control in VB, or my favourite, mciSendString (Media Command
Interface). mciSendString can handle all sorts of file types, and is
relatively easy to use. Only things to really watch out for is that it
requires all path names in short form, and looping a sound will
require either tight timers or subclassing.

Here's a very basic example, with no error checking, paste it into a
form and point the sLongFileName at an MP3 file. It will play
asynchronously as soon as the form opens and be stopped when the form
is closed.

< --- CODE --- >

Option Explicit

Private Declare Function mciSendString Lib "winmm" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, _
ByVal lpstrReturnString 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

Private Sub Form_Load()
Dim sLongFileName As String
Dim sShortFileName As String
Me.Show
sLongFileName = _
"C:\Documents and Settings\{UserName}\My Documents\sample.mp3"
sShortFileName = GetShortName(sLongFileName)
mciSendString "open " & sShortFileName & _
" Type MPEGVideo Alias MyMP3", 0&, 0, 0
mciSendString "play MyMP3", 0&, 0, 0
Me.Print "MP3 should now be playing"
End Sub

Private Function GetShortName( _
ByVal sLongsFileName As String) As String
Dim lRetVal As Long
Dim sShortPathName As String
Dim iLen As Integer
sShortPathName = Space$(255)
iLen = Len(sShortPathName)
lRetVal = GetShortPathName(sLongsFileName, sShortPathName, iLen)
GetShortName = Left$(sShortPathName, lRetVal)
End Function


Private Sub Form_Unload(Cancel As Integer)
mciSendString "close all wait", 0&, 0, 0
End Sub

< --- CODE --- >

--
Alfie [UK]
<http://www.delphia.co.uk/>
The only true handicap is a bad attitude.