Play videos and audio (windows only)

BlitzMax Forums/BlitzMax Tutorials/Play videos and audio (windows only)

Just a small code to show how to play video and audio files on a windows system using BlitzMax and mci.

Its my first try, so code is far from perfect, but it works (at least for me)

ITS WINDOWS ONLY, but perhaps someone might find this usefull for something.

' MCI Video and Audio Player via winmm.dll / Windows ONLY
' Infos on mci commands:
' <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mcisendstring.asp" target="_blank">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_mcisendstring.asp</a>
' Coded by Mirko 'NAPALM' Tocchella. This code is public domain.
'
 
Strict

Import Pub.Win32

Extern "os"
	Function FreeLibrary ( hnd:Int )
	Function GetActiveWindow ()
End Extern

'test
  Graphics 200,200,0
  Local mci:Tmci = New Tmci
  mci.Create()							' init mci
  mci.OpenMediaFile ("test.mpg")
	mci.BreakOn()						' Will make play work until Lmouse pressed
  mci.Play(True, True)		' Wait for break and play fullscreen
	mci.Close()							' Close file
  mci.Free()								' Free dll
  End

Type Tmci

		Field		LibHandle:Int	
		Field		SendHandle:Int
		
		Method GetLibFunction:Int ( symbol:String, fnc:Byte Ptr)
			(Int Ptr(fnc))[0]= SendHandle 	
		End Method 
	
		Method SendString (command:String)
  		Local lCommand (Command$z, xReturn:Int, xReturnLength:Int, xCallBack:Int) "win32"
			GetLibFunction ("mciSendStringA", Varptr lCommand)
			Local res:Int = lCommand (command, Null, Null, Null)
			Return (res)
		End Method

		Method Create ()
			' Get the library handle
			LibHandle = LoadLibraryA("winmm.dll")
			If Not LibHandle Then Throw "Can't load winmm.dll"
			
			' Get the Adress of the SendString function
			SendHandle = Int ( GetProcAddress( LibHandle, "mciSendStringA" ) )
			If Not SendHandle Then Throw "Can't get adress of mciSendStringA"			
		End Method
		
		Method Free ()
			If LibHandle
				If FreeLibrary ( LibHandle )
					LibHandle = 0
				EndIf
			EndIf
		End Method
		
		
		Method OpenMediaFile (FileName:String)
		  Local whnd:Int = GetActiveWindow()
			Local sCommand:String = "open ~q"+FileName+"~q alias MediaFile parent "+whnd
			Local res:Int = SendString (sCommand);
			Return (res)
		End Method
		
		

		Method Play (fullscreen:Int=True, dowait:Int=False)
			Local sCommand:String = "play MediaFile"
			If (fullscreen) sCommand = sCommand + " fullscreen"
			If (dowait) 			sCommand = sCommand + " wait"
			Local res:Int = SendString (sCommand);
			Return (res)
		End Method		
			
		Method Close ()
			Local sCommand:String = "close MediaFile wait"
			Local res:Int = SendString (sCommand);
			Return (res)
		End Method		
			
		Method Configure ()
			Local sCommand:String = "configure MediaFile wait"
			Local res:Int = SendString (sCommand);
			Return (res)
		End Method		
			
		Method BreakOn (vkey:Int=1)
			Local sCommand:String = "break MediaFile on "+vkey
			Local res:Int = SendString (sCommand);
			Return (res)
		End Method		
			
			

End Type