Juan Carlos Rey
Nov 18th, 1999, 07:35 AM
I want my end user to be able to play a series of .wav files which are in a list. I am currently using "shell mplayer.exe" because I want my user to be able to stop, go back, repeat, etc.
The problems I have are:
* "Shell" is asinchronous, I mean my app does not wait for the sound to complete
* If my user does not close Mplayer, and clicks on another .wav file to listen, he will end up with multiple instances of mplayer open at the same time.
* Stupid dumb mplayer starts each time in a different zone of the screen in an apparently random fashion. Can I make it to locate in a defined position, or maybe inside a form?
Thanks in advance.
Aaron Young
Nov 18th, 1999, 10:59 AM
Soemthing like this?
In a Module..
Private Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Private lHwnd As Long
Private Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
lHwnd = hwnd
EnumProc = 0
End Function
Public Function GetThreadWindow(ByVal ThreadID As Long) As Long
Call EnumThreadWindows(ThreadID, AddressOf EnumProc, 0&)
GetThreadWindow = lHwnd
End Function
In the Form..
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetProcessVersion Lib "kernel32" (ByVal ProcessID As Long) As Long
Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As Any, lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOZORDER = &H4
Private Const STARTF_USESHOWWINDOW = &H1
Private Sub Form_Load()
Dim sDir As String
sDir = Dir("C:\Windows\Media\*.wav")
While Len(sDir)
List1.AddItem sDir
sDir = Dir
Wend
End Sub
Private Sub List1_DblClick()
Static tPI As PROCESS_INFORMATION
Dim tSI As STARTUPINFO
Dim tRect As RECT
Dim lHwnd As Long
If tPI.dwProcessId = 0 Or GetProcessVersion(tPI.dwProcessId) = 0 Then
'No Mplayer in use..
tSI.dwFlags = STARTF_USESHOWWINDOW
Call CreateProcess("C:\Windows\MPlayer.exe", " C:\Windows\Media\" & List1, ByVal 0&, ByVal 0&, 0&, 0&, ByVal 0&, "C:\Windows", tSI, tPI)
If tPI.hProcess Then
While WaitForInputIdle(tPI.hProcess, 1)
'Wait for the App to load
Wend
'Locate the Window
lHwnd = GetThreadWindow(tPI.dwThreadId)
Call SetParent(lHwnd, Picture1.hwnd)
Call GetWindowRect(lHwnd, tRect)
Picture1.BorderStyle = vbBSNone
Picture1.Width = ScaleX(tRect.Right - tRect.Left, vbPixels, vbTwips)
Picture1.Height = ScaleY(tRect.Bottom - tRect.Top, vbPixels, vbTwips)
Call SetWindowPos(lHwnd, 0&, 0&, 0&, 0&, 0&, SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOZORDER)
End If
End If
End Sub
------------------
Aaron Young
Analyst Programmer
aarony@redwingsoftware.com
adyoung@win.bright.net
Juan Carlos Rey
Nov 19th, 1999, 12:36 AM
Aaron: your code is working perfectly. Thanks a lot!
I need only a little refinement: Is there a way to automatically
turn mplayer off as soon as it finishes playing?
It is a little annoying to have to close it manually before
listening to another .wav -thanks angain.
[This message has been edited by Juan Carlos Rey (edited 11-19-1999).]