| Home | Previous Lesson: Calling an External Subroutine Next Lesson: Frequently Used MS-Windows APIs |
Windows provides a variety of multimedia services through Media Control Interface (MCI). MCI provides applications with device independent capabilities for controlling audio and visual peripherals. PowerBuilder applications can use MCI to control any of the supported multimedia devices.
The important DLL for MCI communication is MMSYSTEM.DLL under 16-bit and winmm.dll under 32-but environment. MCI includes two interfaces:
|
Low-Level Interface (Command Message Interface): |
Consists of C constants and structures. |
|
High-Level Interface (Command String Interface): |
Textual version of Low-Level Interfaces, that are in easy-to-read format. |
MS-Windows converts Command Strings into Command Messages, before sending them to the MCI Driver for processing. MS-Windows scans both WIN.INI and SYSTEM.INI (Or registry in the 32-bit environment), and loads the appropriate drivers into memory. It then processes the commands sent by the application and sends them to the appropriate driver for execution.
The [MCI EXTENSIONS] section in WIN.INI contains all the required file extensions and the related physical device names.
mid=Sequencer
rmi=Sequencer
wav=waveaudio
avi=AVIVideo
fli=Animation1
flc=Animation1
After examining this .INI file, MMSYSTEM.DLL reads the [MCI] section in SYSTEM.INI to discover the appropriate driver and then loads it. For example, the physical device for MDI extension is a sequencer. The driver that plays the sequencer is MCISEQ.DRV, which is listed under [MCI] section in SYSTEM.INI.
[mci]
cdaudio=mcicda.drv
sequencer=mciseq.drv
waveaudio=mciwave.drv
avivideo=mciavi.drv
videodisc=mcipionr.drv
vcr=mcivisca.drv
Animation1=mciaap.drv
Windows comes with the following drivers:
|
Device Name |
Driver Name |
|
sequencer |
MCISEQ.DRV |
|
waveaudio |
MCIWAVE.DRV |
There are a few others that come with the SDK, and if you need more, you can buy them from third-party vendors. There are two important functions available at the high-level interface, which are sufficient for any PowerBuilder application. They are mciSendString() and mciGetErrorString() under 16-bit environment and mciSendStringA() and mciGetErrorStringA() under 32-bit environment.
We can declare them as external functions, as above, and then use them in our PowerBuilder applications. For example, we could declare mciSendStringA() as follows:
Function Long mciSendStringA(String lpstrCommand, &
Long lpstrReturnString, &
Int uReturnLength, Int hWindCallBack) Library "winmm.dll"
In the above code, replace "winmm.dll" with "mmsystem.dll", if you are working on 16-bit windows. The first parameter is the most used parameter and is the command string to execute, for example, Play c:\windows\ringin.wav.
The following list illustrates the most common commands used as first parameters of mciSendStringA():
Open <Device ID> [Parameters] [Notify] [Wait]
Play <Device ID> [Parameters] [Notify] [Wait]
Close <Device ID> [Notify] [Wait]
The Open command loads the driver into memory (if it isn't already loaded) and assigns a Device ID that can be used to identify the device in subsequent commands. If you don't include a Wait parameter, the application gets control immediately after issuing the command to the driver, instead of waiting until the completion of the operation.
The Play command has many parameters, depending on the device. For example, you can specify the first and last track numbers to play from a CD, such as 'Play CDAUDIO from 6 to 10'. If it is a video device, you can specify it to play fast, slow, reverse and so on. Some other useful basic commands are Record, Pause, Resume, Stop and Seek. Finally, you need to close the device.
We have a window w_script_practice. Right? Open that window and place a CommandButton cb_multimedia. Declare the following external functions by selecting Declare > Local External Functions from the menu.
Function Int mciGetErrorStringA( Long ldwError, &
Ref string lpszErrorText, int cchErrorText) &
Library "winmm"
Function Long mciSendStringA( String lpstrCommand, &
Long lpstrReturnString, Int uReturnLength, &
Int hwndCallBack) Library "winmm"
Write the following code for the Clicked event of cb_multimedia:
String l_path, l_file, l_Error
Long lRetStatus
lRetStatus = GetFileOpenName( "Select File", l_path, &
l_file, "MCI", "Audio Files(*.wav),*.wav" + &
",All Files(*.*),*.*')
If lRetStatus = 0 Then Return 0
lRetStatus = MCISendStringA( "OPEN " + l_path, 0, 0, 0 )
If lRetStatus <> 0 THEN
MCIGetErrorStringA( lRetStatus, l_Error, &
len(l_Error) - 1)
MessageBox( "Open Error", l_Error )
End If
MCISendStringA( "PLAY " + l_path + " WAIT", 0, 0, 0 )
If lRetStatus <> 0 THEN
MCIGetErrorStringA( lRetStatus, l_Error, &
len( l_Error) -1)
MessageBox( "Play Error", l_Error )
End If
MCISendStringA( "CLOSE " + l_path, 0, 0, 0 )
If lRetStatus <> 0 THEN
MCIGetErrorStringA( lRetStatus, l_Error, &
len( l_Error) -1)
MessageBox( "Close Error", l_Error )
End If
In the above code, GetFileOpenName() function allows the user to select a file. So, run this window and play "tada.wav" wave file.
If you don't play a sound file from beginning to the end and if you don't want to write the above script, you can make use of sndPlaySoundA() function. Let's play windows start music when our application starts.
Open w_mdi_frame window and declare the following local external function by selecting Declare > Local External Functions from the menu.
Subroutine sndPlaySoundA( string s_wav_file, &
int uFlags ) Library "winmm"
If you are a windows 16-bit user, replace "winmm" with "mmsystem". In the open event for "w_mdi_frame" window, write the following code.
sndPlaySoundA( "Utopia Windows Start.wav", 1 )
If you are windows 16-bit user, replace sndPlaySoundA with sndPlaySound and also replace the sound file with the appropriate one. Run and test the application.
At run-time, DLLs must be in one of the following directories:
| Home | Previous Lesson: Calling an External Subroutine Next Lesson: Frequently Used MS-Windows APIs |