320 likes | 334 Views
Enhancing Your Applications with Multimedia Elements. What are MM elements? Sound, Images, Digital video, and Dynamic animation. Why are they important for MM applications? To bring an interactive information to the user.
E N D
Enhancing Your Applications with Multimedia Elements What are MM elements? • Sound, Images, Digital video, and Dynamic animation. Why are they important for MM applications? • To bring an interactive information to the user. • To make applications more approachable to the average user and more fun. • To enable MM developers to achieve highly desirable effects by combining these elements. Multimedia Technology Dr. Q Mehdi
Microsoft Multimedia Extension • Microsoft provides very powerful utilities for manipulating sound from Windows. • It is known as MM Extension. • It provides a wide range of audio services. • The extension offers more than 30 Multimedia Control Interface (MCI) commands and 100 low-level functions. Multimedia Technology Dr.Q Mehdi
The Windows API and Visual Basic • Application Programming Interface (API) is simply a set of functions available to an application programmer. • Developed primarily for CProgrammers and are available from within the VB environment. Multimedia Technology Dr.Q Mehdi
API and VB Examples: • The DOS interrupt functions can technically be considered a DOS API. • If you write database programs in Dbase, the Dbase functions can be considered the dBsase API. • When VB program uses OLE Automation to execute an Excel spreadsheet function, it does actually access the Excel API. Multimedia Technology Dr.Q Mehdi
API & VB • It is very easy to access API functions from within VB environment. • VB hides away most of the complexities of Windows programming from the user. • Windows API has literally thousands of functions. • Learning thousands of functions can be time-consuming and difficult to choose the right function for any given case. Multimedia Technology Dr.Q Mehdi
API and VB Windows API functions can be divided into four categories: • Windows Management-User • Graphics Device Interface-GDI • System Services-Kernel • Multimedia - Allows user to play audio, MIDI, music, and digital video. • Note some of API functions can not be used from VB. Multimedia Technology Dr.Q Mehdi
Visual Basic and DLLs • All API functions are implemented as DLLs (Dynamic Link Libraries) and can be called directly or indirectly from any programming language. • What is Dynamic Link Library? Linking refers to the process in which external functions are incorporated into an application. There are two types of linking: Static linking that takes place during creation of the program. Dynamic linking that takes place when the program is running. Multimedia Technology Dr.Q Mehdi
Accessing API from Visual Basic Before using the API function, you should do the following: • Declare the API function. • Tell VB the DLL in which the API function is resided. • Declare the function as shown in the API Text Viewer. Multimedia Technology Dr.Q Mehdi
The API Viewer Application Multimedia Technology Dr.Q Mehdi
API Viewer for VB.NET and C# Courtesy to FreeVBcode.com http://www.freevbcode.com/ShowCode.Asp?ID=3639 Multimedia Technology Dr.Q Mehdi
The Multimedia Control Interface (MCI) • MCI is based on the concept of sending commands to the system to perform specific tasks. • These tasks generally involve reading MM data files, preparing MM devices, and performing other tasks related to record and play MM data. • The system also provides messages back to the calling application regarding the status of the specified commands. Multimedia Technology Dr.Q Mehdi
MCI Control • MCI consists of two levels: 1. Command string interface that consists of simple English-like phrases to control the playing of video, animation, wave audio, and MIDI. Typical example is: open waveaudio • Command string must be parsed into direct MCI commands before being sent to the system. Multimedia Technology Dr.Q Mehdi
MCI Commands 2. Command Message Interface This MCI command provides a lower-level access to MM devices than the command string interface. • Advantage: It can speed up the response time needed to carry out the specified actions by sending the messages directly to the system, rather than asking Windows to parse the command strings into command messages. Multimedia Technology Dr.Q Mehdi
MCI Command Message and VB • For most common uses, the command string interface is more than sufficient for accessing various MM elements. • The command string interface is much easier to implement than the command message interface. • Two primary commands can be used in the command string interface, mciSendString and mciGetErrorString. Multimedia Technology Dr.Q Mehdi
Types of MCI Devices • There are two types (simple and compound): • Simple MCI Devices • It does not require a data file for playback e.g. CD Players and Videodisc players. • The computer is hardly taking part in the process as the devices stream the data from an external source to an output. Multimedia Technology Dr.Q Mehdi
The Compound MCI Devices • Compound MCI devices require a data file for playback and the computer gets involved in the process. • MIDI sequencers and waveform audio players are a typical example of compound MCI devices. • To play an audio file, the samples must be decompressed before being sentto the audio card with the appropriate timing information. Multimedia Technology Dr.Q Mehdi
The high-level sndPlaySound function • sndPlaySound is a high-level function and it is part of Windows MM extension. • It allows for easy playing of any wave file. • The function takes two arguments: 1.The name of the file to be played - nameString 2. A list of options - combination of constants • You can play sound synchronously or asynchronously by using SND_SYNC or SND_ASYNC. Multimedia Technology Dr.Q Mehdi
sndPlaySound Declaration • The sndPlaySound function can be declared from the Windows API viewer of VB application as follows: Private Declare Function sndPlaySound Lib “winmm.dll” Alias “sndPlaySoundA” (ByVal LpszSoundName as String, ByVal uflag as Long) As Long Note:you need to add this declaration to the general declaration section of your VB form. Multimedia Technology Dr.Q Mehdi
sndPlaySound API Function-List of options Parameter (Flags)Description SND_ASYNC = &H1SND_ASYNC specifies that the sound is played asynchronously and the function returnsimmediately after beginning the sound. SND_SYNC = &H0SND_SYNC specifies that the sound is played synchronously and the function does not return until the sound ends. SND_NODEFAULT = &H2 Don't use the Default Sound If the specified sound cannot be found SND_MEMORY = &H4 The sound name points to a memory file SND_LOOP = &H8 Loop the Sound until sndPlaySound is called again with the lpszSoundName$ parameter set to null SND_NOSTOP = &H10 Don't stop any currently playing sound Multimedia Technology Dr.Q Mehdi
Play WAVE file with sndPlaySound • Add the following code to your code (e.g. PlaySound_Click command button function, MouseUp event, etc). Dim errorCode As Long ErrorCode = sndPlaySound(“Tada.wav”, SND_SYNC) • ErrorCode is the return code and of type Long. • The sndPlaySound function allows you to loop the playing of the wave file by setting the second parameter as follows: SND_ASYNC OR SND_LOOP = &H9 Multimedia Technology Dr.Q Mehdi
The MessageBeep Function • MessageBeep is another API function that can be used to play sound associated with one of Windows’s alert events such as Question, Default, Windows Start, etc.). • The syntax of the MessageBeep is: Private Declare Function MessageBeep Lib “User32” (ByVal alertLevel as Long) As Long • The alertLevel is a long integer that indicates which sound to be played. Multimedia Technology Dr.Q Mehdi
MCI Command Strings and MCI Devices • MCI command strings provide an excellent level of control over playing a wave file than sndPlaySound function. • With greater control comes more responsibility for handling the wave audio devices. • All MCI devices currently available are listed in Table 6.1. • All MCI devices recognize plain English-like commands such as play, stop, resume, etc. Multimedia Technology Dr.Q Mehdi
Executing MCI Command With mciSendString and mciGetErrorString functions • mciSendString() function is used to pass an MCI command to the MCI device, the device executes the command and reports back a return code. • Return code can be either Zero or a Number. Zero No Error occurred Number Error occurred Multimedia Technology Dr.Q Mehdi
The mciSendString Arguments Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long ArgumentDescription • MCI_Command as String An English command phrase • ReturnString as String* Contains any return information • ReturnLength as Long Specifies the length of the variable provided for ReturnString • Handle as Long Callback function – Not used in VB * Pointer to a buffer that receives return information such as Bitspersample 8 channels 1, Sample rate set for recording (e.g. 11025 samplespersec ). Also it tells you which mci command failed. If no return information is needed, this parameter can be NULL. Multimedia Technology Dr.Q Mehdi
mciGetErrorStringfunction and error message • Error code returned by mciSendString function can be interpreted by mciGetErrorString function to get a meaningful error description. • Example of error message: “The file cannot be played back on the specified device.” • The basic syntax of all MCI commands is: Verb + Object + Modifier play target device specify samples Multimedia Technology Dr.Q Mehdi
The mciGetErrorString Arguments Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long ArgumentDescription • MCI_ERROR as Long Message number returned from mciSendString • ErrorString as String Contains the ErrorMessage • ReturnLength as Long Specifies the length variable provided for ErrorString Multimedia Technology Dr.Q Mehdi
Play back MCI Command Example • The syntax of the MCI command required to play back the file months.wav is: open months.wav type waveaduio alias months open months ‘open is the verb and months is the object close months ‘ close file and free device for next play back • You can use the modifier to play back only a section of the sound file. Play months from 0 to 2400 Multimedia Technology Dr.Q Mehdi
The MCI command Template 1. Declare the mciSendString ( ) function as follows: Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Multimedia Technology Dr.Q Mehdi
The MCI Command Template 2. Declare the argument returnString as String *255 ‘ or 512 • The returnString argument holds a string with a response from the MCI device (it returns either the requested information or the status of the last message, e.g. if the <Status Track Length 1> command is sent to the system, the returnString parameter returns the length of the first track of the device). • The third parameter specifies the length of the variable provided for returnString. • The last argument in the mciSendString() will always be zero because we can't use callback functions with VB. Multimedia Technology Dr.Q Mehdi
The MCI Command Template 3. Call the mciSendStriong( ) function. Dim errorCode As Long Dim returnStr As String *255 Dim returnCode As Long errorCode = mciSendStringA ("play months", returnStr, 255, 0) • The value returned by the function indicates the success (zero) or the failure (positive) of the call. 4. Call the mciGetErrorString () function to test the message number returned (errorCode) from mciSendStriong( ) function. This allows you to retrieve a text description of mciSendStriong( ) return values. Multimedia Technology Dr.Q Mehdi
Retrieving Error From MIC Devices • You can improve the performance of your application by adding the mciGetErrorString function to your code. • The mciGetErrorString function examines the Error code returned by the mciSendCommand or mciSendString function and returns a string that describes the specified MCI error code. • To detect the MCI error, you must carry out the following steps: • Declare the mciGetErrorString() function. • Test the message number returned from mciSendString () function with mciGetErrorString() function as follows: Dim returnStr, errorStr As String * 255 ‘0r 512 errorCode = mciSendStringA("play audio", returnStr, 255, 0) returnCode = mciGetErrorStringA(errorCode, errorStr, 255) MsgBox errorStr Multimedia Technology Dr.Q Mehdi
Enhancing your applications With Sound • Thanks for your attention. • Any question? • There will be a workshop session on sound next week. Multimedia Technology Dr.Q Mehdi