PDA

View Full Version : MediaPlayer in ThinBasic form



Rocco
29-09-2010, 21:14
I'm trying to use the Windows Media Player in ThinBasic (beta 1.8.6.0), I based my script on "ThinGames", also in this forum.

I know that COM/ActiveX support is not complete, but I'm trying to understand why this script doesn't work with the latest Media Player. When I try to call any method or set property, the application crashes. Maybe it's a problem with threading or other...

Any help?


Uses "UI"
Uses "COM"

Declare Function UnregisterClass Lib "USER32.DLL" Alias "UnregisterClassA" (ByVal lpClassName As Asciiz, ByVal hInstance As DWord) As Long
Declare Function GetModuleHandle Lib "KERNEL32.DLL" Alias "GetModuleHandleA" (ByVal lpModuleName As Asciiz) As DWord

Declare Function AtlAxWinInit Lib "ATL.DLL" Alias "AtlAxWinInit" () As Long
Declare Function AtlAxGetControl Lib "ATL.DLL" Alias "AtlAxGetControl" (ByVal hControl As DWord, ByRef pObject As DWord) As Long

Function AtlAxWinTerm () As Long
UnregisterClass ("AtlAxWin", GetModuleHandle(%NULL))
End Function

Dim Msg As Long
Dim wParam As Long
Dim lParam As Long
Dim hDlg As Long
Dim wx, hy As Long

Dim hCtrl As Long

%Max_Param = 3
Dim vParam(%Max_Param) As Variant

%ID_OCX = 1001
%ID_BUTTON = 1002

Dim oMediaPlayer As DWord

$CLASS = "WMPlayer.OCX"

AtlAxWinInit

Dialog New 0, "Media Player", -1, -1, 220, 300, %DS_CENTER, 0 To hDlg
Control Add "AtlAxWin", hDlg, %ID_OCX, $CLASS, 10, 50, 200, 200, %WS_CHILD Or %WS_VISIBLE
Control Add Button hDlg, %ID_BUTTON, "Play", 10, 10, 50, 25, Call onClick

Dialog Get Client hdlg To wx, hy

Control Set Resize hDlg, %ID_OCX, 1, 1, 1, 1

Control Handle hDlg, %ID_OCX To hCtrl
AtlAxGetControl(hCtrl, oMediaPlayer)

Dialog Show Modeless hDlg

While IsWindow(hDlg)
Msg = GetMessage(hDlg, wParam, lParam)

Select Case Msg

Case %WM_INITDIALOG

Case %WM_COMMAND

Case %WM_SYSCOMMAND
Select Case wParam
Case %SC_CLOSE
Exit While
End Select

Case Else

End Select

Wend

Dialog End hDlg
AtlAxWinTerm

CallBack Function onClick()
If CBMSG = %WM_COMMAND Then
Select Case CBCTLMSG
Case %BN_CLICKED
If CBCTL = %ID_BUTTON Then
vParam(1) = "http://www.mididatabase.com/20100702/punk/ramones/I_Wanna_Be_Sedated.mid"
COM_EXECUTE(oMediaPlayer, "Url", %TB_DISPATCH_PROPERTYPUT, 1, vParam, 0)
End If
End Select

End If
End Function

Petr Schreiber
29-09-2010, 23:06
Hi Rocco,

I am sadly not expert on COM, but if you like to render video inside dialog, you could use header file from here:
MCI Video Wrapper (http://community.thinbasic.com/index.php?topic=1914.msg14158#msg14158)

COM module was developed by Roberto Biancchi, I hope he will come back to development some time in future, but I have no news from him.
Eros had plan to take different approach on COM, using the "normal" dotted syntax, but when this will be completed I cannot say.

I hope in the meantime the MCI solution could be of interest to you - it plays both video and music.

For sound, there is also TBASS module, quite advanced one, based on famous BASS library.


Petr

Michael Clease
29-09-2010, 23:10
Hello Rocco

Here is a version of your script just using callbacks and with a quit option.

I cant see what causes the crash but my knowledge of COM is near zero, is the value returned by "AtlAxGetControl(hCtrl, oMediaPlayer) " correct?

can I also suggest using the Syntax Highlight (ThinBasic) when posting code as your previous post contained hidden characters.


Uses "UI","COM"

Declare Function UnregisterClass Lib "USER32.DLL" Alias "UnregisterClassA" (ByVal lpClassName As Asciiz, ByVal hInstance As DWord) As Long
Declare Function GetModuleHandle Lib "KERNEL32.DLL" Alias "GetModuleHandleA" (ByVal lpModuleName As Asciiz) As DWord

Declare Function AtlAxWinInit Lib "ATL.DLL" Alias "AtlAxWinInit" () As Long
Declare Function AtlAxGetControl Lib "ATL.DLL" Alias "AtlAxGetControl" (ByVal hControl As DWord, ByRef pObject As DWord) As Long

Function AtlAxWinTerm () As Long
UnregisterClass ("AtlAxWin", GetModuleHandle(%NULL))
End Function

Begin Const
%Max_Param = 1
%ID_OCX = 1001
%ID_Play
%ID_QUIT
$myCLASS = "WMPlayer.OCX"
End Const


Dim Msg As Long
Dim wParam As Long
Dim lParam As Long
Dim hDlg As DWord
Dim wx, hy As Long
Dim hCtrl As Long
Dim vParam(%Max_Param) As Variant
Dim oMediaPlayer As DWord


AtlAxWinInit

Dialog New 0, "Media Player", -1, -1, 220, 300, %DS_CENTER, 0 To hDlg

Dialog Show Modal hDlg, Call dlgProc
AtlAxWinTerm

CallBack Function dlgProc()
' -- Test for messages
Select Case CBMSG

Case %WM_INITDIALOG

Control Add "AtlAxWin", CBHNDL, %ID_OCX, $myCLASS, 10, 50, 200, 200, %WS_CHILD Or %WS_VISIBLE
Control Add Button CBHNDL, %ID_PLAY, "Play", 10, 10, 50, 25, Call Play
Control Add Button CBHNDL, %ID_QUIT, "QUIT", 70, 10, 50, 25, Call Quit
Dialog Get Client CBHNDL To wx, hy
Control Set Resize CBHNDL, %ID_OCX, 1, 1, 1, 1
Control Handle CBHNDL, %ID_OCX To hCtrl
AtlAxGetControl(hCtrl, oMediaPlayer)
Case %WM_COMMAND
Case %WM_TIMER
Case %WM_CLOSE
Case %WM_DESTROY

End Select
End Function

CallBack Function Quit()
If CBMSG = %WM_COMMAND And CBCTLMSG = %BN_CLICKED And CBCTL = %ID_QUIT Then
Dialog End CBHNDL
End If
End Function

CallBack Function Play()
If CBMSG = %WM_COMMAND And CBCTLMSG = %BN_CLICKED And CBCTL = %ID_PLAY Then
vParam(1) = "http://www.mididatabase.com/20100702/punk/ramones/I_Wanna_Be_Sedated.mid"
COM_EXECUTE(oMediaPlayer, "Url", %TB_DISPATCH_PROPERTYPUT, 1, vParam, 0)
End If
End Function