// MCI AVI example
// From the MCI Command Strings help (mcistrwh.hlp), which has the complete string descriptions.
// Return value from mciSendString() is either a 0 for sucess, or an error code.
// mciSendString() actually returns a double, but CA-dBFast has a current
// bug that it will GPF on the return of the DLL call returning a double.
// Use integer for now.
declare pfunc mciSendString returning integer in dll mmsystem.dll

// get a textual error message from the error code.
declare pfunc mciGetErrorString returning integer in dll mmsystem.dll

retinfo = space(255)
reterror = space(255)
errornum = 0

sAVIFile = dbfmenu('Video File,*.AVI','Select Video File')
if empty(sAVIFile)
	quit
endif

// replace 'waveaudio' with 'sequencer' for MIDI, or 'WaveAudio' for WAV...
// Installed device driver names can be found in the [MCI] section of the system.ini
// 'alias' just specifies an alternate name for the opened device 
errornum = mciSendString("open avivideo!" + alltrim(sAVIFile) + " alias avi", tolong(0), toint(0), toint(0))
if (errornum) // error handling example
	mciGetErrorString(tolong(errornum), @reterror, toint(255))
	warning("MCI Error", reterror, 385)
	quit
endif

set window type to 1+1024
create window 'AVI' at 2,2 to 15,35
hWnd = str(HWND())

errornum = mciSendString("window avi handle "+hWnd, tolong(0), toint(0), toint(0))
if (errornum) // error handling example
	mciGetErrorString(tolong(errornum), @reterror, toint(255))
	warning("MCI Error", reterror, 385)
	mciSendString("close avi", tolong(0), toint(0), toint(0))
	quit
endif

// Determine if the device driver can stretch the video
//? mciSendString("capability avi can stretch", @retinfo, toint(255), toint(0))
//? retinfo

// get the sampling rate for a WAV
//? mciSendString("status wave samplespersec", @retinfo, toint(255), toint(0))
//? retinfo + " Hz"

// Force the AVI to be played in the 'AVI' window
errornum =  mciSendString("put avi destination", @retinfo, toint(255), toint(0))
if (errornum) // error handling example
	mciGetErrorString(tolong(errornum), @reterror, toint(255))
	warning("MCI Error", reterror, 385)
	mciSendString("close avi", tolong(0), toint(0), toint(0))
	quit
endif

//wait ""

// play the avi
errornum = mciSendString("play avi", tolong(0), toint(0), toint(0))
if (errornum) // error handling example
	mciGetErrorString(tolong(errornum), @reterror, toint(255))
	warning("MCI Error", reterror, 385)
	mciSendString("close avi", tolong(0), toint(0), toint(0))
	quit
endif

wait ""

// close the device
errornum = mciSendString("close avi", tolong(0), toint(0), toint(0))

wait