Home All Groups Group Topic Archive Search About

How do you code Asynchronous MP3 playing with VB6/API?

Author
2 Jun 2005 12:43 PM
Timothy Casey
Is it possible to play an MP3 file asynchronously using VB6 code and/or API?

If so how is the playback made asynchronous?

Thanks in Advance...

--
Timothy Casey GPEMC! >> 11950 is the num***@fieldcraft.biz 2email
          Terms & conditions apply.  See www.fieldcraft.biz/GPEMC
Discover the most advanced speed comprehension application at:
www.fieldcraft.biz/shop  <BRef http://www.fieldcraft.biz/ki.htm >

Author
2 Jun 2005 3:03 PM
Norm Cook
Not sure what you mean by play asynchronously, but this code
will play an mp3 until you stop it or it ends.  If you want it to keep
playing after you exit the app, just comment out the code
in the Form_Unload event

Option Explicit
Private Const WS_CHILD = &H40000000
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 Declare Function mciGetErrorString Lib "winmm.dll" Alias
"mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String,
ByVal uLength 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()
SendMCIString "close all", True
PlayMP3 "<path to mp3 file>"    'change this***********
End Sub
Private Sub Form_Unload(Cancel As Integer)
SendMCIString "close all", True
End Sub
'Wrapper for mcisendstring
Private Function SendMCIString(ByVal Cmd As String, Optional ByVal CheckErr
As Boolean = False) As Boolean
Dim Ret As Long
Dim Buff As String * 260
Ret = mciSendString(Cmd, 0&, 0&, 0&)
If Ret Then
  If CheckErr Then
   mciGetErrorString Ret, Buff, 260
   MsgBox Left$(Buff, InStr(Buff, Chr$(0)))
  End If
End If
SendMCIString = CBool(Ret)
End Function
Private Function ShortPath(ByVal LongPath As String) As String
Dim Ret As Long
Dim Buff As String * 260
Ret = GetShortPathName(LongPath, Buff, 260)
ShortPath = Left$(Buff, Ret)
End Function
Private Sub PlayMP3(ByVal MP3Path As String)
Dim CmdString As String
MP3Path = ShortPath(MP3Path)
If Len(MP3Path) Then
  CmdString = "open " & MP3Path & _
    " type MPEGVideo Alias mp3 parent " _
    & hWnd & " Style " & WS_CHILD
  If Not SendMCIString(CmdString, True) Then
   SendMCIString "play mp3", False
  End If
Else
  MsgBox "Problem with Mp3 Path"
End If
End Sub

'other functions
Private Sub StopMP3()
SendMCIString "stop mp3", False
End Sub
Private Sub PauseMP3()
SendMCIString "pause mp3", False
End Sub
Private Sub ResumeMP3()
SendMCIString "resume mp3", False
End Sub

Show quoteHide quote
"Timothy Casey" <ReplaceOhWithZeroNumbersOnly11***@fieldcraft.biz> wrote in
message news:429efc3c$1_1@news.iprimus.com.au...
> Is it possible to play an MP3 file asynchronously using VB6 code and/or
API?
>
> If so how is the playback made asynchronous?
>
> Thanks in Advance...
>
> --
> Timothy Casey GPEMC! >> 11950 is the num***@fieldcraft.biz 2email
>           Terms & conditions apply.  See www.fieldcraft.biz/GPEMC
> Discover the most advanced speed comprehension application at:
> www.fieldcraft.biz/shop  <BRef http://www.fieldcraft.biz/ki.htm >
>
>
Author
2 Jun 2005 3:11 PM
MikeD
"Timothy Casey" <ReplaceOhWithZeroNumbersOnly11***@fieldcraft.biz> wrote in
message news:429efc3c$1_1@news.iprimus.com.au...
> Is it possible to play an MP3 file asynchronously using VB6 code and/or
> API?
>
> If so how is the playback made asynchronous?


Easiest way is to use MCI.  By default, it will play asynchronously.  Here's
an example.  Copy and paste into a form:

-----BEGIN CODE
Option Explicit

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 Declare Function GetShortPathName Lib "kernel32" Alias
"GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As
String, ByVal cchBuffer As Long) As Long

Private Const MAX_PATH As Long = 260

Private Function GetShortFileName(ByVal sFileName As String) As String

    Dim sBuffer As String

    sBuffer = String$(MAX_PATH, vbNullChar)

    Call GetShortPathName(sFileName, sBuffer, MAX_PATH)

    GetShortFileName = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)

End Function

Private Sub Form_Load()

    Dim sMP3File As String

    'Hard-coded for example purposes only.
    sMP3File = "H:\My Documents\My Music\70s\Rock\TED NUGENT -
STRANGLEHOLD.mp3"
    sMP3File = GetShortFileName(sMP3File)

    Call mciSendString("play " & sMP3File, vbNullString, 0&, 0&)

End Sub

Private Sub Form_Unload(Cancel As Integer)

    Call mciSendString("close all", vbNullString, 0&, 0&)

End Sub
-----END CODE

If you wanted to play it synchronously, you'd include the "wait" parameter
in the command string, like this:

    Call mciSendString("play " & sMP3File & " wait", vbNullString, 0&, 0&)

Note that you should always get the short path and filename because spaces
in long names cause problems (MCI uses spaces to separate parameters) and
there is a limit on the length of the command string itself (long names can
easily exceed that limit).  When played as above, MCI automatically opens
and closes the file, so you don't have to explicitly do so yourself.
However, it's best to always close all devices (as done above in the Unload
event).

For more information on all the MCI command strings, see the following:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_multimedia_command_strings.asp


--
Mike
Microsoft MVP Visual Basic
Author
3 Jun 2005 5:11 AM
Timothy Casey
"Norm Cook" <normcookNOSPAM@cableone.net> wrote in message
news:eArE5Q4ZFHA.1368@tk2msftngp13.phx.gbl...
[SNIP]

"MikeD" <nob***@nowhere.edu> wrote in message
news:%23p0ERV4ZFHA.796@TK2MSFTNGP09.phx.gbl...
[SNIP]

Thankyou very much for your timely response.

Your code does work asynchronously in the application concerned, so the
sound comes up at the correct time relative to the visual cues on the form.
This is exactly what I was looking for.

Thanks again!    :^)

--
Timothy Casey GPEMC! >> 11950 is the num***@fieldcraft.biz 2email
          Terms & conditions apply.  See www.fieldcraft.biz/GPEMC
Discover the most advanced speed comprehension application at:
www.fieldcraft.biz/shop  <BRef http://www.fieldcraft.biz/ki.htm >